My input string is a PM time:
log(start);
// Sunday, January 09, 2011 6:30:00 PM
I'm using Joda Time's pattern syntax as follows to parse the 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
Cause joda-time is deprecated.
Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat. Low level - builder.
Joda-Time uses immutable objects. So rather than change the time zone ("mutate"), we instantiate a new DateTime object based on the old but with the desired difference (some other time zone).
By tackling this problem head-on, Joda-Time became the de facto standard date and time library for Java prior to Java SE 8. Note that from Java SE 8 onwards, users are asked to migrate to java. time (JSR-310) - a core part of the JDK which replaces this project.
Your parse string contains "H" which is telling your parser to interpret the value as a 24-hour hour of day (0..23). So the 6 is interpreted as the 6th hour of the day. In the morning. The AM that's printed is because the overall date parsed is considered to be in the morning.
If you want to use 12-hour time, change your format string to:
"EEEE, MMMM dd, yyyy h:mm:ss aa".
'h' will be interpreted as a 12-hour hour of day (1..12)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With