A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/zoneddatetime-oflocal-method-in-java-with-examples/ below:

ZonedDateTime ofLocal() method in Java with Examples

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



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4