ZonedDateTime ofLocal() method in Java with Examples
Last Updated :
10 Apr, 2023
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
import java.time.*;
public class GFG {
public static void main(String[] args)
{
LocalDateTime lt
= LocalDateTime
.parse(" 2019 - 01 -29T23: 55 : 59.00 ");
ZoneOffset zoneOffset
= ZoneOffset.ofHours( 11 );
ZoneId zone
= ZoneId.of("Europe/Paris");
ZonedDateTime zt
= ZonedDateTime
.ofLocal(lt, zone, zoneOffset);
System.out.println("ZonedDateTime is "
+ zt);
}
}
|
Output:
ZonedDateTime is 2019-01-29T23:55:59+01:00[Europe/Paris]
Program 2:
Java
import java.time.*;
public class GFG {
public static void main(String[] args)
{
LocalDateTime lt
= LocalDateTime
.parse(" 2019 - 01 -29T23: 55 : 59.00 ");
ZoneOffset zoneOffset
= ZoneOffset.ofHours( 10 );
ZoneId zone
= ZoneId.of("Australia/Darwin");
ZonedDateTime zt
= ZonedDateTime
.ofLocal(lt, zone, zoneOffset);
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)
Share your thoughts in the comments
Please Login to comment...