The Date
class in Java can return time in milliseconds, and from which timezone does it return, current or from UTC?
4 answers
The Data
object returns you the system time of your machine and only. You can programmatically change the time on the machine.
Date dataCurrent=new Date(); System.out.println(dataCurrent); TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles")); Date dataNewZone=new Date(); System.out.println(dataNewZone);
Program output
Wed Sep 21 17:47:40 MSK 2016
Wed Sep 21 07:47:40 PDT 2016
The time in milliseconds is the same for all time zones.
Local (What is on your PC)
Now another class is responsible for this - Calendar
The Data object returns you the system time of your virtual machine . Ie, opening the question completely, time, taking into account your timezone.
Local system time (system time) The local system time is obtained by adding to the Greenwich time the difference values with Greenwich Mean Time and the difference of years specified in the time zone of the system. The system time of day contains exactly the local system time. The concepts of system time and local system time are equivalent.
You can check this statement by calling
System.out.println(Calendar.getInstance().getTime());
And digging into src how this method is implemented ...
UPD In response to the author's comment. off documentation note the following lines:
It is not necessary to follow the Java Virtual Machine. Nearly all modern operating systems assume that 1 day = 24 × 60 × 60 = 86400 seconds in all cases. In UTC, however, it is called a leap second. It was 61 seconds long, thanks to an added leap second. The computer clocks are not accurate.
in addition to interesting information about the second time correction. You may find that a Date object provides you with time in the format of Coordinated Universal Time ...
- oneI read somewhere that Date.getTime () returns the time in milliseconds of January 1, 1970 and the timezone is always UTC 0 by default, I don’t remember whether it really is or whether it seemed to me so - J Mas
java.util.Date
does not store information about the time zone, only the current time zone is always used, which is taken from OC. If you need to store the time zone value, you should use the java.util.Calendar
class, see setTimeZone (TimeZone value) and getTimeZone ()