开发者

How to properly format unusual date string using Java SimpleDateFormat?

I've got date in following format:

Pon Cze 07, 2011 9:42 pm

It's Polish equivalent of English date:

Mon Jun 07, 2011 9:42 pm

开发者_JAVA百科I'm using following SimpleDateFormat matcher:

SimpleDateFormat("EEE MMM dd, yyyy H:mm a", new Locale("pl", "PL"))

But date cannot be parsed because of AssertionFailedError. I've try some other solution, but no one works for me. Do you got any ideas what I do wrong?


There are two problems:

  1. The day is wt, and not Pon, in the pl locale
  2. The AM/PM indicator is not taken into account because you used H (which means hour from 0 to 23) instead of h.

I would just parse from the first character after the first white space (to avoid parsing "Pon"), adn replace H by h. in the pattern:

DateFormat df = new SimpleDateFormat("MMM dd, yyyy h:mm a", new Locale("pl", "PL"));
Date d = df.parse(s.substring(s.indexOf(' ') + 1));


It seems, that the day of the week is only represented by two Letters, whatever the reason for this might be. Try this Code and check the Output

    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd, yyyy H:mm a", new Locale("pl", "PL"));
    GregorianCalendar gc = new GregorianCalendar(new Locale("pl", "PL"));
    gc.setTime(new Date(System.currentTimeMillis()));
    System.out.println(sdf.format(gc.getTime()));

My Output is: "Wt wrz 27, 2011 11:05 AM"

So maybe if you try two lettered weekdays, it could work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜