Search code examples
javaspring-bootepochdocker-containerlocaldate

Getting different timestamp(epoch time) locally and remotely while using LocalDateTime in SpringBoot


I am printing Epoch time using LocalDateTime like this

System.out.println("timestamp "+LocalDateTime.now().atZone(ZoneId.of("Asia/Kolkata")).toInstant().toEpochMilli());

So whenever I run this piece of code locally in Intellij I get the proper timestamp with the Kolkata timezone which is approx 1559461130527. This is around Sunday, June 2, 2019 1:23:50.527 PM Kolkata time

Then I have a server with docker container and whenever I run this same piece of code there I get a totally different timestamp which is approx 1559439902340. This is around Sunday, June 2, 2019 7:30:02.340 AM.

What am I missing, isn't the server supposed to print the timestamp according to the specified time zone. Any help would be appreciated. Thanks.


Solution

  • You should use the zone offset in the now() statement.

    For example to get UTC time I use LocalDateTime.now(ZoneOffset.UTC);

    In your case you can use directly the ZoneId for your time zone

    LocalDateTime.now(ZoneId.of("Asia/Kolkata"));