Open In App

ZonedDateTime ofLocal() method in Java with Examples

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

ofLocal() method of an ZonedDateTime class used to create an instance of ZonedDateTime from the Local date-time using ZoneId 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




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


Output:

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

Program 2: 

Java




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


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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads