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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

ZonedDateTime is 2019-01-29T23:55:59+01:00[Europe/Paris]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

ZonedDateTime is 2019-01-29T23:55:59+09:30[Australia/Darwin]

References: https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#ofInstant(java.time.LocalDateTime, java.time.ZoneOffset, java.time.ZoneId)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.