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:
- The day is
wt
, and notPon
, in the pl locale - The AM/PM indicator is not taken into account because you used
H
(which means hour from 0 to 23) instead ofh
.
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
精彩评论