Reputation: 1556
I've found this post which kinda explains half of my problem. which is why my AM symbol is not what I expected. Unable to parse DateTime-string with AM/PM marker
SimpleDateFormat sdfBefore = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a", Locale.US);
// I tried format with 2 [a] as well , MM/dd/yyyy hh:mm:ss aa
//SimpleDateFormat sdfAfter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
System.out.println("Debug: test6 = "+sdfBefore.format(System.currentTimeMillis()));
System.out.println("Debug: test7 = "+sdfBefore.parse("05/26/2014 06:57:07 a.m."));
System.out.println("Debug: test8 = "+sdfBefore.parse("05/26/2014 06:57:08 A.M."));
System.out.println("Debug: test9 = "+sdfBefore.parse("05/26/2014 06:57:09 AM"));
And here's my result:
Debug: test6 = 05/26/2014 07:30:01 a.m.
Debug: test7 = Mon May 26 06:57:07 GMT 2014
Debug: test8 = Mon May 26 06:57:08 GMT 2014
Error: java.text.ParseException: invalid Date syntax in "05/26/2014 06:57:09 AM"
Locale I tried so far:
German = vorm.
US/ENGLISH/<default>/Canada = a.m.
chinese = ? <--probably cause my sonsole doesn't support it
Every example I found online says the english/US locale should be the correct one for the symbol "AM" and "PM"
does anyone notice anything wrong here?
Upvotes: 1
Views: 1320
Reputation: 1556
Ok, so I found a brute force way to fix it, but I'll still be greatful if anyone knows what's the root cause of it.
DateFormatSymbols sym = new DateFormatSymbols(Locale.US);
sym.setAmPmStrings(new String[] { "AM", "PM"});
SimpleDateFormat sdfBefore = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a", sym);
System.out.println("Debug: test6 = "+sdfBefore.format(System.currentTimeMillis()));
System.out.println("Debug: test9 = "+sdfBefore.parse("05/26/2014 06:57:09 AM"));
Result:
Debug: test6 = 05/26/2014 07:47:44 AM
Debug: test9 = Mon May 26 06:57:09 GMT 2014
Upvotes: 2