ADVERTISEMENT
ADVERTISEMENT

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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.