ZonedDateTime ofInstant() method in Java with Examples
Last Updated : 03 May, 2023
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)
Similar Reads
ZonedDateTime of() Method in Java with Examples In ZonedDateTime class, there are three types of() method depending upon the parameters passed to it. of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone) of() method of a ZonedDateTime class used to get an instance of ZonedDateTime from a year, mo
3 min read
ZonedDateTime ofStrict() Method in Java with Examples ofStrict() method of ZonedDateTime class used to create an instance of ZonedDateTime strictly validating the combination of local date-time, offset and zone ID where all three Local date-time, ZoneOffset and ZoneId are passed as parameter.If the offset is invalid, an exception is thrown. Syntax: pub
2 min read
ZonedDateTime minusNanos() method in Java with Examples minusNanos() method of a ZonedDateTime class used to subtract the number of nanoseconds from this ZonedDateTime and return a copy of ZonedDateTime after subtraction.This method works on the instant timeline and subtracting one nanosecond return the timeline of one nanosecond earlier. Note that this
2 min read
ZonedDateTime toString() method in Java with Examples The toString() method of a ZonedDateTime class is used to return this ZonedDateTime as a String. The string consists of the LocalDateTime followed by the ZoneOffset. If the ZoneId is not the same as the offset, then the ID is output. Syntax: public String toString() Parameters: This method does not
1 min read
ZonedDateTime minusMonths() method in Java with Examples minusMonths() method of a ZonedDateTime class used to subtracts the number of months in this ZonedDateTime and return a copy of ZonedDateTime after subtraction. This instance is immutable and unaffected by this method call. Syntax: public ZonedDateTime minusMonths(long months) Parameters: This metho
2 min read