Java – Converts pre-1900 timestamps in Java

Converts pre-1900 timestamps in Java… here is a solution to the problem.

Converts pre-1900 timestamps in Java

My Android app communicates with an API and it gives me the following timestamp: -2209161600. Convert to datetime, which should be 12-30-1899

00:00:00

The problem is that I try to convert this timestamp using the default libraries threetenbp and jodatime, but I always get the same error result, using the Europe/Paris time zone: 12-30-1899 00:09:21

Why is this?

Edit: e.g. jodatime

DateTime dt = new DateTime(-2209161600000L, DateTimeZone.forID("Europe/Paris"));  dt: "1899-12-30T00:09:21.000+00:09:21"

Solution

I think I was in Why is the offset for a time-zone different to the JDK? The answer is found in the FAQ:

… affects date-times before the modern time-zone system was introduced. The time-zone data is obtained from the time-zone database . The database contains information on “Local Mean Time” (LMT) which is the local time that would have been observed at the location following the Sun’s movements.

Joda-Time uses the LMT information for all times prior to the first time-zone offset being chosen in a location. …

In other words, the database does not have an entry for that time, so it uses local standard time (e.g. 0:09:21 for Paris and -0:14:44 1 for Madrid).

System.out.println(new DateTime(-2209161600000L, DateTimeZone.forID("Europe/Paris")));
System.out.println(new DateTime(-2209161600000L, DateTimeZone.forID("Europe/Madrid")));

Will be printed

1899-12-30T00:09:21.000+00:09:21
1899-12-29T23:45:16.000-00:14:44

Solution: Look at what the time needs to do, and use it if UTC is enough

new DateTime(-2209161600000L, DateTimeZone.forID("UTC"))  // 1899-12-30T00:00:00.000Z

Or just the standard java.time class, like

Instant.ofEpochSecond(-2209161600L)
Instant.ofEpochMilli(-2209161600000L)

1http://home.kpn.nl/vanadovv/time/TZworld.html#eur

Related Problems and Solutions