ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset)
creates an instance of ZonedDateTime from a local date-time using the preferred offset if possible.
ofLocal
has the following syntax.
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset)
The following example shows how to use ofLocal
.
import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; /* w ww . j a v a2 s .co 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.
Create a zoned date time
import java.time.LocalDateTime; import java.time.Month; import java.time.ZoneId; import java.time.ZonedDateTime; /* w w w . ja v a 2 s. c o 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.