Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimeFormatter weekday seems off by one

Tags:

People also ask

Should DateTimeFormatter be static?

DateTimeFormatter itself has numerous static constants like this. It is even recommended to store the (immutable) formatter in a static constant (for performance reasons because constructing a formatter can be quite expensive).

Is Java time format DateTimeFormatter thread-safe?

Yes, it is: DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well. Implementation Requirements: This class is immutable and thread-safe.

What can I use instead of SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

What is iso_instant?

The ISO_INSTANT formatter is a special case formatter designed to work with Instant . If you are using a ZonedDateTime you should use a different formatter, such as ISO_DATE_TIME or ISO_ZONED_DATE_TIME .


I'm porting an existing application from Joda-Time to Java 8 java.time.

I ran into a problem where parsing a date/time string that contains a 'day of week' value triggered an exception in my unit tests.

When parsing:

2016-12-21 20:50:25 Wednesday December +0000 3

using format:

yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'EEEE' 'MMMM' 'ZZ' 'e

I get:

java.time.format.DateTimeParseException: 
Text '2016-12-21 20:50:25 Wednesday December +0000 3' 
could not be parsed: Conflict found: 
Field DayOfWeek 3 differs from DayOfWeek 2 derived from 2016-12-21

When letting the DateTimeFormatter indicate what it expects:

String logline     = "2016-12-21 20:50:25 Wednesday December +0000";
String format      = "yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'EEEE' 'MMMM' 'ZZ";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format).withLocale(Locale.ENGLISH);;
ZonedDateTime dateTime = formatter.parse(logline, ZonedDateTime::from);

format      = "yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'EEEE' 'MMMM' 'ZZ' 'e";
formatter = DateTimeFormatter.ofPattern(format).withLocale(Locale.ENGLISH);
System.out.println(formatter.format(dateTime));

I now get this output:

2016-12-21 20:50:25 Wednesday December +0000 4

So effectively the root cause of the problem is that the e flag in Joda-Time considers Monday to be 1 yet the Java 8 java.time considers Monday to be 0.

Now for the patterns that java.time.DateTimeFormatter supports I find in both the Oracle documentation and in JSR-310 this:

e/c     localized day-of-week       number/text       2; 02; Tue; Tuesday; T

This explicit example of 2 and 'Tuesday' leads me to believe that Wednesday should also in java.time be 3 instead of 4.

What is wrong here? Do I misunderstand? Is this a bug in Java 8?