Use it to convert Instant.now()
to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z
where Z
stands for Zulu
time and represents UTC
.
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now.toEpochMilli());
}
}
You can compare this result with that of the C# code at C# PlayGround.
Note: Avoid specifying a timezone with the 3-letter abbreviation. A timezone should be specified with a name in the format, Region/City e.g. ZoneId.of("Europe/London")
. With this convention, the ZoneId
for UTC
can be specified with ZoneId.of("Etc/UTC")
. A timezone specified in terms of UTC[+/-]Offset
can be specified as Etc/GMT[+/-]Offset
e.g. ZoneId.of("Etc/GMT+1")
, ZoneId.of("Etc/GMT+1")
etc.
There are some exceptional cases as well e.g. to specify the timezone of Turkey
, you can specify
ZoneId.of("Turkey")
However, it is recommended (Thanks to Ole V.V.) to use the same pattern as described above i.e. the recommended way of representing the timezone of Turkey is:
ZoneId.of("Asia/Istanbul")
or
ZoneId.of("Europe/Istanbul")
Probably, Istanbul
being referred with two regions/continents is the reason why it is still returned by ZoneId.getAvailableZoneIds()
.
The following code will give you all the available ZoneId
s:
// Get the set of all time zone IDs.
Set<String> allZones = ZoneId.getAvailableZoneIds();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…