2

I just want to understand the currentTimeMillis()/1000L method in System class. As per my understanding that this method will return current time in seconds. I have a doubt here If I run the same code in Local jvm (IST) , and remote box(Sanjose) will return different values as output?

Can someone explain me how does it work if we run the code in different JVM which is USA and India what will be the output. Does it return same output? or different?

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84
  • 1
    It uses UTC no matter what time zone you are in. So, yes it will be the same anywhere in the world. See also: https://stackoverflow.com/q/17271039/669576 – 001 Aug 27 '18 at 18:55
  • Something important most people left out was the fact that none of this will matter if both computers haven’t synced with an NTP recently. – vandench Aug 27 '18 at 22:07
  • If you want seconds rather than milliseconds, I would prefer `Instant.now().getEpochSecond()`. It’s clearer to read that doing your own division by 1000. Making your own time conversions is a poor habit. – Ole V.V. Aug 28 '18 at 06:03

3 Answers3

4

It returns number of ms since the beginning of unix epoch, which is time-zone independent. Therefore, it should return the same output.

irahavoi
  • 330
  • 2
  • 10
3

The code will return the same output because .currentTimeMillis() function uses the UTC timezone rather than a local timezone of the JVM.

See Oracle's documentation: https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis()

Hayes Roach
  • 121
  • 10
0

it gives the amount of milliseconds that have passed since Epoch time which is 00:00:00 on 1 January 1970. It is also called Unix time. Learn more about it here https://en.wikipedia.org/wiki/Unix_time

Maurice
  • 6,698
  • 9
  • 47
  • 104