Home » Java » datetime – Java OffsetDateTime showing strange year value-Exceptionshub

datetime – Java OffsetDateTime showing strange year value-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

Per the IntelliJ debugger, the OffsetDateTime value is showing like:

+51434-04-02T15:28:41Z

Any idea how to parse the year part or make sense of it? The raw date is actually in milliseconds when I view it on the DB. I have no access to the team that is saving these dates. Also, when accessing the data via my tests it’s showing the same when I do a log.debug.

How to&Answers:

Seeing the timestamp, I think I know why this is happening. Some part of the code has incorrectly interpreted the timestamp 1559909381182. This timestamp represents a the number of milliseconds since the epoch, but your code has interpreted it as a number of seconds. Compare these two lines:

System.out.println(Instant.ofEpochSecond(1559909381182L));
System.out.println(Instant.ofEpochMilli(1559909381182L));

The first will print out a date thousands of years in the future, while the second line will print a more reasonable date.

You should try and find where you have interpreted it as a second.