Java Convert LocalDate and LocalTime to ZonedDateTime with Specified Time Zone
Tags: Java LocalDate Java LocalTime Java ZonedDateTime Java 8
In this Java tutorial, we learn how to convert the LocalDate and LocalTime values into a ZonedDateTime value in specified time zone or a system default time zone in Java programming language.
How to convert LocalDate and LocalTime to ZonedDateTime
In Java, with given LocalDate, LocalTime values and a specified ZoneId value we can use the ZonedDateTime.of(LocalDate date, LocalTime time, ZoneId zone) method to convert it to a ZonedDateTime value as the following example Java program.
ZonedDateTimeExample1.java
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZonedDateTimeExample1 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 8, 1);
LocalTime localTime = LocalTime.of(8, 30, 15);
ZoneId zoneId = ZoneId.of("Australia/Sydney");
// Convert LocalDate, LocalTime to ZonedDateTime with specified time zone
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, localTime, zoneId);
System.out.println(zonedDateTime);
}
}
2022-08-01T08:30:15+10:00[Australia/Sydney]
The following Java example code to show to convert to ZonedDateTime in system default time zone using the ZoneId.systemDefault() method to get time zone.
ZonedDateTimeExample2.java
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZonedDateTimeExample2 {
public static void main(String... args) {
LocalDate localDate = LocalDate.of(2022, 8, 1);
LocalTime localTime = LocalTime.of(8, 30, 15);
ZoneId zoneId = ZoneId.systemDefault();
// Convert LocalDate, LocalTime to ZonedDateTime with system default time zone
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, localTime, zoneId);
System.out.println(zonedDateTime);
}
}
2022-08-01T08:30:15+07:00[Asia/Bangkok]
Happy Coding 😊