Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 673
Tree Avatar asked Jan 13 '11 18:01

Tree


People also ask

Is Joda-Time deprecated?

Cause joda-time is deprecated.

What is Joda-Time format?

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.

Does Joda DateTime have time zones?

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).

What is the replacement of Joda-Time Library in Java 8?

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.


1 Answers

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)

like image 67
robert_x44 Avatar answered Sep 28 '22 09:09

robert_x44