Search code examples
javatimeepoch

How to change the default epoch of 1970-01-01T00:00:00Z for LocalDateTime


In the LocalDateTime class, there is a method to convert epoch second and nano second to UTC time.

It looks like:

LocalDateTime dateTime = LocalDateTime.ofEpochSecond()

However, it obtains an instance of LocalDateTime using seconds from the epoch of 1970-01-01T00:00:00Z.

Is there a way to modify 1970 to 1980 as the default instance?

I believe I need it to be changed since:

epoch seconds = 667662644 
epoch nanoseconds = 1165598

should come out to:

13:50:44.001165598 03/03/2001

but I am getting:

13:50:44.001165598 2/27/1991

Solution

  • Figure out the offset you need and add it to all the epoch seconds values before converting to a `LocalDateTime'.

    The offset can be calculated once and then hard-coded as

    public static final int EPOCH_OFFSET = nnnn;
    

    Sample:

        Instant GPS = Instant.parse("1980-01-06T00:00:00Z");
        long    GPS_EPOCH = GPS.toEpochMilli();
        System.out.printf("%s %,d\n",GPS.toString(), GPS_EPOCH);
    

    Output:

    1980-01-06T00:00:00Z 315,964,800,000