I've run into a strange behavior of SimpleDateFormat, i don't know how to deal with.
I need to parse a date in a specific format (Day of Week, then day, then Month, then Year, Then time). However, I've run into a behaviour, when parsing a date gives me a very strnge result (other date). Here is a small, self-contained example, and it's output on my machine.
public static void main(String[] args) throws Exception {
test("E YYYY kk:mm:ss");
test("E d YYYY kk:mm:ss");
test("E d MMMM YYYY kk:mm:ss");
}
public static void test(String format) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date now = new Date();
System.out.println(now);
String formattedNow = sdf.format(now);
System.out.println(formattedNow);
Date parsedFormattedNow = sdf.parse(formattedNow);
String formattedParsedNow = sdf.format(parsedFormattedNow);
System.out.println(formattedParsedNow);
System.out.println(formattedNow.equals(formattedParsedNow));
}
Output:
Sat Apr 27 13:48:07 MSK 2013
Sat 2013 13:48:07
Sat 2013 13:48:07
true
Sat Apr 27 13:48:07 MSK 2013
Sat 27 2013 13:48:07
Sat 5 2013 13:48:07
false
Sat Apr 27 13:48:07 MSK 2013
Sat 27 April 2013 13:48:07
Sat 5 January 2013 13:48:07
false
Why do then 27 transforms into 5, and April to January?