0

I'm trying to modify the existing java code to output the data in milliseconds instead of seconds.

existing code which return current time in GMT in seconds:

currentTime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);

output currentTime = 1566311076

using epoch converter it says

GMT: Tuesday, August 20, 2019 2:24:36 PM
Your time zone: Tuesday, August 20, 2019 7:24:36 AM GMT-07:00 DST

My attempt to modify the java code to return current time in GMT in millisec is able to get current system time in millisec, however how do I offset the result to GMT time.

currentTime = ZonedDateTime.now().toInstant().toEpochMilli();

output currentTime = 1566336256566

Assuming that this timestamp is in milliseconds

GMT: Tuesday, August 20, 2019 9:24:16.566 PM
Your time zone: Tuesday, August 20, 2019 2:24:16.566 PM GMT-07:00 DST

do you know , will greatly appreaciate that. Thanks!

m.antkowicz
  • 13,268
  • 18
  • 37
  • Possible near-duplicate of [How to find epoch format current time of GMT using java](https://stackoverflow.com/questions/51640313/how-to-find-epoch-format-current-time-of-gmt-using-java) – Ole V.V. Aug 21 '19 at 05:23

2 Answers2

1

Convert to Instant first:

currentTime = LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli()

Demo

System.out.println(LocalDateTime.now().toEpochSecond(ZoneOffset.UTC));
System.out.println(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli());

Output

1566323773
1566323773363
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

Why do it in a more complicated way when a simple one exists?

    long currentTime;
    currentTime = System.currentTimeMillis();
    System.out.println(currentTime);

Example output from just now:

1566369127348

I am a strong proponent of using the modern java.time classes including ZonedDateTime (maybe not so much LocalDateTime). However in this case a simple method from when Java was born gives us what we want. I see no issues with using it. If you do want to use java.time, use Instant:

    currentTime = Instant.now().toEpochMilli();
    System.out.println(currentTime);

1566369127348

What went wrong in your code?

Curiously your old code was incorrect. Your new attempt gives the correct number of milliseconds since the epoch. Your old code was:

    currentTime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);

This takes the current wall-clock time in your time zone, or more precisely, in the default time zone of your JVM. It then incorrectly assumes that the time is in UTC and converts it to seconds since the epoch based on this assumption (of course, if the JVM’s time zone had been UTC, you would have happened to get the correct result).

I recommend you always pass a time zone (if not a clock) to a now method to make your expectations of the time zone used explicit. The no-arg now uses the JVM’s default time zone, which is unreliable because the setting can be changed unexpectedly, and may cause confusion. If the time zone had been stated in the above code line, we would all have been able to see at a glance whether it agreed with the UTC assumed afterward or not. The exception is Instant.now(): it doesn’t need a time zone since it gives the same result in all time zones.

Output from your old code on my computer in Europe/Copenhagen time zone was:

1566376327

You can see that it doesn’t agree with the millisecond values we got before (it’s two hours ahead in this case; in your case it was 7 hours behind).

Your question seems to have been posted some time around 10 PM (22:00) GMT (3 PM at offset -07:00), so none of your results fit well with that time. Either some hours went by from running your code to posting your question; or your computer clock is set incorrectly.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161