I am wondering if System.currentTimeMillis() accounts for daylight savings on android.
Let's say I call
x = System.currentTimeMillis();
and then ten seconds later, the os switches the clock back an hour because of daylight savings.
And now I call
y = System.currentTimeMillis();
Which will be larger, x
or y
?
I ask this because if I manually change the clock, x
is greater than y
. So I wonder if daylight savings is accounted for when calculating the long value returned by System.currentTimeMillis()
. Can't seem to find anything in the docs.
currentTimeMillis() actually give the time accurate to the nearest millisecond on Linux, Mac OS and Windows (and since which versions - I know, for example, that Windows only used to be accurate to the nearest 15/16 milliseconds).
currentTimeMillis. Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
The milliseconds since epoch are not influenced by timezones and daylight saving time (daylight saving time just changed the timezone with -1 / +1). The milliseconds/seconds since epoch are (always?) in UTC (or GMT + 0). Yes - It is always based on UTC, excluding leap seconds.
ZonedDateTime takes daylight savings into account. That's what differs it from LocalDateTime.
if System.currentTimeMillis() accounts for daylight savings
No.
Daylight Saving Time (DST) is irrelevant to System.currentTimeMillis
.
The call to System.currentTimeMillis
gives you a number of milliseconds since the first moment of 1970 in UTC, 1970-01-01T00:00:00Z
. No adjustments for Leap Second.
DST is defined, and redefined, on the whim of bored/uneducated/crazy politicians. DST applies only to their jurisdiction in one or more particular time zones.
DST cutovers are anomalies, disrupting wall-clock time for a region’s citizens. But space-time does not bend or warp with a DST cutover, time continues to flow smoothly. That flow of time is measured by System.currentTimeMillis
without regard to machinations of local politicians.
Instant
The System.currentTimeMillis()
method is now obsolete. To get the current moment, use java.time.Instant.now
.
Instant now = Instant.now() ;
While a Instant
can hold nanoseconds, in Java 9 and later you are likely to see the current moment captured in microseconds. (Java 8 used milliseconds, before a new implementation of Clock
in Java 9 and later.)
In contrast, the System.currentTimeMillis
call is limited to milliseconds.
To get a count of milliseconds since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z, call Instant::toEpochMilli
. Of course, beware of data loss as any microseconds or nanoseconds are truncated from the result as milliseconds.
long millisecondsSinceEpoch = Instant.now().toEpochMilli() ;
Instant
represents a moment in UTC. So Daylight Saving Time (DST) and similar anomalies caused by politicians have no effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With