My code was working just fine. Today suddenly I started getting this exception - org.threeten.bp.DateTimeException: Field DayOfMonth cannot be printed as the value 1872095944 max width is 2
This is my simple code :
LocalDateTime date = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd - MM - uuuu");
String sDate = date.format(formatter);//EXCEPTION THROWN HERE
Why this problem suddenly?
EDIT
This seems to be an intermediate problem. It crashes sometimes and works fine on other times. No clues as to what is happening. A
Are there any recent changes in the api. I don't see any u option in allowed pattern list.
It is less a formatting problem (here just a symptom) but rather a problem how an instance of
LocalDateTime
is created. Root cause is simply thatLocalDateTime.now()
seems to produce a day-of-month completely out of bounds in some rare cases. This problem is probably related to this issue on the issue tracker of threeten-bp.Keep in mind that the method
now()
must do an epoch conversion in the background and finally callLocalDate.ofEpochDay(...)
. So if your clock produces an unusal epoch value in millis since Unix epoch then this can affectnow()
, too. And your formatter just fetches the day-of-month from yourLocalDateTime
by effectively callinggetDayOfMonth()
(really via field access inTemporalAccessor
). The source code in question:Most interesting and suspicious is the fact that only the year is validated, but not the month or day-of-month. And indeed, have a look at this bizarre result containing four parts separated by minus-chars (???):
Obviously the library code is broken for some exotic epoch-day-numbers which might be produced by your clock unfortunately. I have also tested the same code in Java-8, with the same wrong result.
Update:
The original code of
LocalDate.ofEpochDay(long)
shown so far is definitely broken, also because of the fact that there is no check for numerical/arithmetic overflow. For example: An input likeLong.MAX_VALUE
causes the expressionepochDay + DAYS_0000_TO_1970
to overflow and to change the sign to negative. Similar, the inputLong.MIN_VALUE
will finally overflow when using the expression400 * zeroDay
. And I fear that this is not the only problem of shown code. For comparison: A correct implementation of the gregorian algorithm would rather look like in my own time library.Side note:
By help of my library Time4J I have analyzed that given test input above would yield a year far out of bounds as defined in threeten-bp, too (range is -999999999 until +999999999):
I am not quite sure what you can do to solve the problem.
First thing is surely to log all inputs produced by your clock, relate them to the observed buggy behaviour of threeten-bp and to do some research why your clock goes sometimes mad.
About the bug in threeten-bp (and Java-8!), you can just hope that the threeten-bp-project team will soon fix it (or rather Oracle!). The input causing the problems is probably wrong anyway so you should best catch the exception and log it with the extra message that the clock is wrong (as root cause).