ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) example
Description
ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset)
creates an instance of ZonedDateTime from a local date-time using the preferred offset if possible.
Syntax
ofLocal
has the following syntax.
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset)
Example
The following example shows how to use ofLocal
.
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
//from w w w .j av a2s. c o m
public class Main {
public static void main(String[] args) {
ZonedDateTime z = ZonedDateTime.ofLocal(LocalDateTime.now(),ZoneId.systemDefault(),ZoneOffset.UTC);
System.out.println(z);
}
}
The code above generates the following result.
Example 2
Create a zoned date time
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
//from w w w . j a v a 2 s. co m
public class Main {
public static void main(String[] args) {
LocalDateTime leaving = LocalDateTime.of(2013, Month.JULY, 20, 19, 30);
ZoneId leavingZone = ZoneId.of("America/Los_Angeles");
ZonedDateTime departure = ZonedDateTime.of(leaving, leavingZone);
System.out.println(departure);
}
}
The code above generates the following result.