Why does Joda change PM in my input string to AM?

My input line is PM time:

log(start); // Sunday, January 09, 2011 6:30:00 PM 

I use the Joda time pattern syntax as follows to parse a DateTime:

  DateTimeFormatter parser1 = DateTimeFormat.forPattern("EEEE, MMMM dd, yyyy H:mm:ss aa"); DateTime startTime = parser1.parseDateTime(start); 

So why is my output string AM?

  log(parser1.print(startTime)); // Sunday, January 09, 2011 6:30:00 AM 
+8
java datetime parsing jodatime
source share
2 answers

The parsing line contains an “H” that tells your parser to interpret the value as a 24-hour hour of the day (0..23). Thus, 6 is interpreted as the 6th hour of the day. In the morning. The AM that is being printed is that the general date in question is considered morning.

If you want to use 12 hour time, change the format string to:

 "EEEE, MMMM dd, yyyy h:mm:ss aa". 

'h' will be interpreted as the 12-hour hour of the day (1..12)

+25
source share

H will give you a value of 0-23 every day, so maybe he will read it when he sees 6 and determines that it should be AM?

Try using lower case h to get the time zone of the day.

+2
source share

Source: https://habr.com/ru/post/651041/


All Articles