ZonedDateTime ofLocal() method in Java with Examples
ofLocal() method of an ZonedDateTime class used to create an instance of ZonedDateTime from the Local date-time using ZonmeId and the preferred offset if possible where all three localDateTime, ZoneOffset and ZoneId are passed as parameter.The local date-time is resolved to a single instant on the time-line achieved by finding a valid offset from UTC/Greenwich for the local date-time as defined by the rules of the zone ID.
Syntax:
public static ZonedDateTime ofLocal(LocalDateTime localDateTime,
ZoneId zone,
ZoneOffset preferredOffset)
Parameters: This method accepts three parameters Instant which is the local date-time, zone which is the time-zone and ZoneOffset which is the zone offset.
Return value: This method returns the zoned date-time.
Below programs illustrate the ofLocal() method:
Program 1:
// Java program to demonstrate // ZonedDateTime.ofLocal() method import java.time.*; public class GFG { public static void main(String[] args) { // create localDateTime object LocalDateTime lt = LocalDateTime .parse("2019-01-29T23:55:59.00"); // create ZoneOffset ZoneOffset zoneOffset = ZoneOffset.ofHours(11); // create a ZonID ZoneId zone = ZoneId.of("Europe/Paris"); // apply ofLocal method // of ZonedDateTime class ZonedDateTime zt = ZonedDateTime .ofLocal(lt, zone, zoneOffset); // print the result System.out.println("ZonedDateTime is " + zt); } } |
ZonedDateTime is 2019-01-29T23:55:59+01:00[Europe/Paris]
Program 2:
// Java program to demonstrate // ZonedDateTime.ofLocal() method import java.time.*; public class GFG { public static void main(String[] args) { // create localDateTime object LocalDateTime lt = LocalDateTime .parse("2019-01-29T23:55:59.00"); // create ZoneOffset ZoneOffset zoneOffset = ZoneOffset.ofHours(10); // create a ZonID ZoneId zone = ZoneId.of("Australia/Darwin"); // apply ofLocal method // of ZonedDateTime class ZonedDateTime zt = ZonedDateTime .ofLocal(lt, zone, zoneOffset); // print the result System.out.println("ZonedDateTime is " + zt); } } |
ZonedDateTime is 2019-01-29T23:55:59+09:30[Australia/Darwin]
Recommended Posts:
- ZonedDateTime now() Method in Java with Examples
- ZonedDateTime until() Method in Java with Examples
- ZonedDateTime get() method in Java with Examples
- ZonedDateTime of() Method in Java with Examples
- ZonedDateTime with() Method in Java with Examples
- ZonedDateTime plus() method in Java with Examples
- ZonedDateTime isSupported() method in Java with Examples
- ZonedDateTime withMonth() method in Java with Examples
- ZonedDateTime withNano() method in Java with Examples
- ZonedDateTime toOffsetDateTime() method in Java with Examples
- ZonedDateTime toLocalDateTime() method in Java with Examples
- ZonedDateTime withDayOfMonth() method in Java with Examples
- ZonedDateTime withHour() method in Java with Examples
- ZonedDateTime withMinute() method in Java with Examples
- ZonedDateTime toString() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

