java.time.ZoneId
is a class in Java’s Date-Time API used to represent a time zone identifier. This identifier is used to get a ZoneRules
, which then can be used to convert between an Instant
and a LocalDateTime
.
Here is how you can use the ZoneId
class in a simple way:
package org.kodejava.datetime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZoneIdExample {
public static void main(String[] args) {
// Get the system default ZoneId
ZoneId defaultZoneId = ZoneId.systemDefault();
System.out.println("System Default TimeZone : " + defaultZoneId);
// Get ZoneId instance using the specified zone ID as a string
ZoneId londonZoneId = ZoneId.of("Europe/London");
System.out.println("London ZoneId : " + londonZoneId);
// Get ZonedDateTime using ZoneId
ZonedDateTime zonedDateTimeInLondon = ZonedDateTime.now(londonZoneId);
System.out.println("Current date and time in London: " + zonedDateTimeInLondon);
}
}
Output:
System Default TimeZone : Asia/Makassar
London ZoneId : Europe/London
Current date and time in London: 2024-01-19T06:43:23.076855Z[Europe/London]
In the above code:
ZoneId.systemDefault()
is used to get the system defaultZoneId
.ZoneId.of(String zoneId)
is used to get aZoneId
instance using the specified zone ID as a string. You can get all available zone IDs by callingZoneId.getAvailableZoneIds()
.ZonedDateTime.now(ZoneId zoneId)
is used to get the current date and time in the specified time zone.
Please note that the ZoneId
is immutable and thread-safe, it ensures the class can be used safely in multithreaded systems.
Latest posts by Wayan (see all)
- How do I create a servlet filter to make secure cookies? - April 18, 2024
- How do I configure secure cookies using web.xml? - April 18, 2024
- How do I create a table with multiple header in iText 8? - March 16, 2024