Java LocalDateTime Class | ofInstant() Method with Example

LocalDateTime Class ofInstant() method: Here, we are going to learn about the ofInstant() method of LocalDateTime Class with its syntax and example.
Submitted by Preeti Jain, on June 08, 2020

LocalDateTime Class ofInstant() method

  • ofInstant() method is available in java.time package.
  • ofInstant() method is used to create an instance of LocalDateTime from the given instant and zone.
  • ofInstant() method may throw an exception at the time of representing the object.
    DateTimeException – This exception may throw when this LocalDateTime value reaches out of the min or max instant.
  • ofInstant() method is a static method and it is accessible with the class name and if we try to access these methods with the class object then we will not get an error.

Syntax:

public static LocalDateTime ofInstant(Instant ins, ZoneId zo);

Parameter(s):

  • Instant ins – represents the instant by which we are creating LocalDateTime.
  • ZoneId zo – represents the time zone.

Return value:

The return type of this method is LocalDateTime, it returns the LocalDateTime that holds the value creates from the given Instant and zone.

Example:

// Java program to demonstrate the example 
// of ofInstant(Instant ins, ZoneId zo) method 
// of LocalDateTime

import java.time.*;

public class OfInstantOfLocalDateTime {
    public static void main(String args[]) {
        // Instantiates a ZoneId and
        // Instant
        ZoneId zo = ZoneId.of("Africa/Accra");
        Instant ins = Instant.parse("2006-04-03T05:10:15.00Z");

        // Here, this method creates an instance of 
        // LocalDateTime by using the given
        // Instant and ZoneId
        LocalDateTime da_ti = LocalDateTime.ofInstant(ins, zo);

        // Display da_ti
        System.out.print("LocalDateTime.ofInstant(ins,zo): ");
        System.out.println(da_ti);
    }
}

Output

LocalDateTime.ofInstant(ins,zo): 2006-04-03T05:10:15


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.