Open In App

ZonedDateTime ofInstant() method in Java with Examples

Last Updated : 03 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In ZonedDateTime class, there are two types of ofInstant() method depending upon the parameters passed to it.

ofInstant(Instant instant, ZoneId zone)

ofInstant() method of an ZonedDateTime class used to create an instance of ZonedDateTime from an Instant and zoneId passed as parameter to this method.It is very helpful method when you have a Instant object and zoneId and you want to create a Clock containing both means ZonedDateTime. 

Syntax:

public static ZonedDateTime ofInstant(Instant instant,
                                      ZoneId zone)

Parameters: This method accepts two parameters Instant which is the instant to create the date-time from, It can not be null and zone which is the time-zone. 

Return value: This method returns the zoned date-time. 

Exception: This method throws following Exceptions:

  • DateTimeException – if the result exceeds the supported range.

Below programs illustrate the ofInstant() method: 

Program 1: 

Java




// Java program to demonstrate
// ZonedDateTime.ofInstant() method
 
import java.time.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create Instant object
        Instant lt
            = Instant
                  .parse("2018-10-20T16:55:30.00Z");
 
        // create a ZoneID
        ZoneId zone = ZoneId.of("Europe/Paris");
 
        // apply ofInstant method
        // of ZonedDateTime class
        ZonedDateTime zt = ZonedDateTime
                               .ofInstant(lt, zone);
 
        // print the result
        System.out.println("ZonedDateTime is "
                           + zt);
    }
}


Output:

ZonedDateTime is 2018-10-20T18:55:30+02:00[Europe/Paris]

Program 2: 

Java




// Java program to demonstrate
// ZonedDateTime.ofInstant() method
 
import java.time.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // create Instant object
        Instant lt
            = Instant
                  .parse("2019-01-29T23:55:59.00Z");
 
        // create a ZoneID
        ZoneId zone
            = ZoneId.of("Australia/Darwin");
 
        // apply ofInstant method
        // of ZonedDateTime class
        ZonedDateTime zt
            = ZonedDateTime
                  .ofInstant(lt, zone);
 
        // print the result
        System.out.println("ZonedDateTime is "
                           + zt);
    }
}


Output:

ZonedDateTime is 2019-01-30T09:25:59+09:30[Australia/Darwin]

ofInstant(LocalDateTime localDateTime,, ZoneOffset offset, ZoneId zone)

ofInstant() method of an ZonedDateTime class used to create an instance of ZonedDateTime from the localDateTime formed by combining the local date-time and offset where all three Instant, ZoneOffset and ZoneId are passed as parameter. 

Syntax:

public static ZonedDateTime ofInstant(LocalDateTime localDateTime,, 
                                ZoneOffset offset, ZoneId zone)

Parameters: This method accepts three parameters localDateTime which is the local date-time, ZoneOffset which is the zone offset and zone which is the time-zone. 

Return value: This method returns the zoned date-time. 

Below programs illustrate the ofInstant() method: 

Program 1: 

Java




// Java program to demonstrate
// ZonedDateTime.ofInstant() 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 ofInstant method
        // of ZonedDateTime class
        ZonedDateTime zt
            = ZonedDateTime
                  .ofInstant(lt, zoneOffset, zone);
 
        // print the result
        System.out.println("ZonedDateTime is "
                           + zt);
    }
}


Output:

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

Program 2: 

Java




// Java program to demonstrate
// ZonedDateTime.ofInstant() 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 ofInstant method
        // of ZonedDateTime class
        ZonedDateTime zt
            = ZonedDateTime
                  .ofInstant(lt, zoneOffset, zone);
 
        // print the result
        System.out.println("ZonedDateTime is "
                           + zt);
    }
}


Output:

ZonedDateTime is 2019-01-29T23:25: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) https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#ofInstant(java.time.LocalDateTime, java.time.ZoneId)



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

Similar Reads