Reputation: 27203
I know that System.currentTimeMillis()
gives the time in milliseconds since the epoch and it is sensitive to the wall clock time of the system. I also know that it is not advisable to use System.currentTimeMillis()
to calculate elapsed time in a program that measures time.
Java library have provided System.nanoTime()
for that purpose.
I have two specific questions for System.currentTimeMillis()
:
Upvotes: 8
Views: 3611
Reputation: 44071
The clear answer to your two questions is: No, no.
In supplement to the answer of @ok about dst effects, System.currentTimeMillis()
does not count leap seconds. In theory and practise it depends on the operational system. Since most OS and the Java VM included rather follow POSIX standard the consequence is (also by interoperability and behavioural compatibility issues) that leap seconds are not even allowed to have any effect on System.currentTimeMillis()
.
You can also easily verify this by comparing the value of System.currentTimeMillis()
(divided by 1000) with your own calculation of SI seconds elapsed since 1972-01-01T00:00:00. The difference must be now 25 seconds.
UPDATE from the discussion below:
Leap seconds do not have effect on System.currentTimeMillis()
in the long term (not counted), but maybe in the short term. I consider this as irrelevant however because this time source is not designed for achieving high accuracy and time stability.
The output of this time source is even subject to sudden changes up to minutes (for example if a pc was offline for a long time). And if people are going to use this time source and experience hard problems during a leap second then they will almost for sure also experience (harder) problems during any resync of this time source with an external time source like a NTP server.
Upvotes: 1
Reputation: 691943
The value returned by System.currentTimeMillis()
is the number of milliseconds that have elapsed since the epoch. This number of milliseconds is just that: a number of milliseconds.
Let's say that on a given day, your country has decided to go from 1:00 to 2:00 instantly, due to DST. Does that mean that 1 hour has elapsed between 1:00 and 2:00? No. 0 milliseconds have elapsed. Your country has simply decided that the millisecond value X was represented as 1:00, and that the millisecond X + 1 was represented at 2:00. The representation of the time doesn't change what milliseconds are.
Upvotes: 11