How do I convert LocalDate to ZonedDateTime?

You can use the atStartOfDay() method from LocalDate class to convert a LocalDate into a LocalDateTime. Then, you need to convert LocalDateTime to a ZonedDateTime using the atZone() method.

Here is an example:

package org.kodejava.datetime;

import java.time.*;

public class LocalDateToZonedDateTimeExample {
    public static void main(String[] args) {
        // Create a LocalDate
        LocalDate date = LocalDate.of(2023, Month.JULY, 9);
        System.out.println("LocalDate: " + date);

        // Convert LocalDate to LocalDateTime
        LocalDateTime dateTime = date.atStartOfDay();
        System.out.println("LocalDateTime: " + dateTime);

        // Convert LocalDateTime to ZonedDateTime
        ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
        System.out.println("ZonedDateTime: " + zonedDateTime);
    }
}

Output:

LocalDate: 2023-07-09
LocalDateTime: 2023-07-09T00:00
ZonedDateTime: 2023-07-09T00:00+08:00[Asia/Makassar]

In this example, we’re creating a LocalDate for July 9, 2023. Then we’re converting it to a LocalDateTime, and then to a ZonedDateTime. The atStartOfDay() method returns a LocalDateTime set to the start of the day (00:00) on the date of this LocalDate. The atZone() method then takes the ZoneId and returns a ZonedDateTime representing the start of the day in that timezone.

The ZoneId.systemDefault() returns the system default time zone. If you want to convert it to a specific time zone, you can specify the timezone as a string, like this: ZoneId.of("America/New_York").

What is ZoneRules class of Java Date-Time API?

The ZoneRules class in Java’s Date-Time API is used to encapsulate the set of rules defining how the zone offset varies for a single time zone.

The information in this class is typically derived from the IANA Time Zone Database (TZDB). The rules model the data traditionally contained in the ‘zic’ compiled data files of information from the TZDB.

An instance of ZoneRules is obtained from a ZoneId using the ZoneId.getRules() method.

Here is a simple example of how to use the ZoneRules class:

package org.kodejava.datetime;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.zone.ZoneRules;

public class ZoneRulesExample {
    public static void main(String[] args) {
        // Get ZoneId for "Europe/Paris"
        ZoneId zoneId = ZoneId.of("Europe/Paris");
        System.out.println("ZoneId : " + zoneId);

        // Get ZoneRules associated with the ZoneId
        ZoneRules zoneRules = zoneId.getRules();
        System.out.println("ZoneRules : " + zoneRules);

        // Get the standard offset
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneOffset offset = zoneRules.getOffset(localDateTime);
        System.out.println("Offset for " + localDateTime + " is: " + offset);
    }
}

Here we are using LocalDateTime.now() to get the current time and the getOffset(LocalDateTime) method on ZoneRules to find the offset for that particular time. The API guarantees immutability and thread-safety of ZoneRules class.

This example will output:

  • The ZoneId which will be “Europe/Paris”
  • The ZoneRules for the “Europe/Paris” time zone
  • The ZoneOffset for the current LocalDateTime. This offset is the difference in time between the “Europe/Paris” time zone and UTC at the time provided.

Output:

ZoneId : Europe/Paris
ZoneRules : ZoneRules[currentStandardOffset=+01:00]
Offset for 2024-01-19T15:55:14.156977 is: +01:00

How do I use java.time.ZoneId class?

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 default ZoneId.
  • ZoneId.of(String zoneId) is used to get a ZoneId instance using the specified zone ID as a string. You can get all available zone IDs by calling ZoneId.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.

How do I convert java.util.TimeZone to java.time.ZoneId?

The following code snippet will show you how to convert the old java.util.TimeZone to java.time.ZoneId introduced in Java 8. In the first line of our main() method we get the default timezone using the TimeZone.getDefault() and convert it to ZoneId by calling the toZoneId() method. In the second example we create the TimeZone object by calling the getTimeZone() and pass the string of timezone id. To convert it to ZoneId we call the toZoneId() method.

package org.kodejava.datetime;

import java.time.ZoneId;
import java.util.TimeZone;

public class TimeZoneToZoneId {
    public static void main(String[] args) {
        ZoneId zoneId = TimeZone.getDefault().toZoneId();
        System.out.println("zoneId = " + zoneId);

        TimeZone timeZoneUsPacific = TimeZone.getTimeZone("US/Pacific");
        ZoneId zoneIdUsPacific = timeZoneUsPacific.toZoneId();
        System.out.println("zoneIdUsPacific = " + zoneIdUsPacific);
    }
}

This snippet prints the following output:

zoneId = Asia/Shanghai
zoneIdUsPacific = US/Pacific

To convert the other way around you can do it like the following code snippet. Below we convert the ZoneId to TimeZone by using the TimeZone.getTimeZone() method and pass the ZoneId.systemDefault() which return the system default timezone. Or we can create ZoneId using the ZoneId.of() method and specify the timezone id and then pass it to the getTimeZone() method of the TimeZone class.

package org.kodejava.datetime;

import java.time.ZoneId;
import java.util.TimeZone;

public class ZoneIdToTimeZone {
    public static void main(String[] args) {
        TimeZone timeZone = TimeZone.getTimeZone(ZoneId.systemDefault());
        System.out.println("timeZone = " + timeZone.getDisplayName());

        TimeZone timeZoneUsPacific = TimeZone.getTimeZone(ZoneId.of("US/Pacific"));
        System.out.println("timeZoneUsPacific = " + timeZoneUsPacific.getDisplayName());
    }
}

And here are the output of the code snippet above:

timeZone = China Standard Time
timeZoneUsPacific = Pacific Standard Time

How do I get a list of all TimeZones Ids using Java 8?

To retrieve a list of all available time zones ids we can call the java.time.ZoneId static method getAvailableZoneIds(). This method return a Set of string of all zone ids. The format of the zone id are “{area}/{city}”. You can use these ids of string to create the ZoneId object using the ZoneId.of() static method.

package org.kodejava.datetime;

import java.time.ZoneId;
import java.time.format.TextStyle;
import java.util.Locale;
import java.util.Set;

public class GetAllTimeZoneIds {
    public static void main(String[] args) {
        Set<String> zoneIds = ZoneId.getAvailableZoneIds();
        for (String id : zoneIds) {
            ZoneId zoneId = ZoneId.of(id);
            System.out.println("id          = " + id);
            System.out.println("displayName = " +
                    zoneId.getDisplayName(TextStyle.FULL, Locale.US));
        }
    }
}

Here are some zone IDs printed out to the console:

id          = Asia/Aden
displayName = Arabian Time
id          = America/Cuiaba
displayName = Amazon Time
id          = Etc/GMT+9
displayName = GMT-9:00
id          = Etc/GMT+8
displayName = GMT-8:00
id          = Africa/Nairobi
displayName = Eastern Africa Time
...
...
...
id          = Europe/Nicosia
displayName = Eastern European Time
id          = Pacific/Guadalcanal
displayName = Solomon Is. Time
id          = Europe/Athens
displayName = Eastern European Time
id          = US/Pacific
displayName = Pacific Time
id          = Europe/Monaco
displayName = Central European Time