ZonedDateTime ofInstant() method in Java with Examples
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 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 ZonID 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); } } |
ZonedDateTime is 2018-10-20T18:55:30+02:00[Europe/Paris]
Program 2:
// 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 ZonID 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); } } |
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 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 ZonID 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); } } |
ZonedDateTime is 2019-01-29T13:55:59+01:00[Europe/Paris]
Program 2:
// 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 ZonID 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); } } |
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)
Recommended Posts:
- OffsetTime ofInstant() method in Java with examples
- LocalTime ofInstant() method in Java with Examples
- ZonedDateTime get() method in Java with Examples
- ZonedDateTime until() Method in Java with Examples
- ZonedDateTime plus() method in Java with Examples
- ZonedDateTime now() Method in Java with Examples
- ZonedDateTime with() Method in Java with Examples
- ZonedDateTime of() Method in Java with Examples
- ZonedDateTime minus() method in Java with Examples
- ZonedDateTime withZoneSameInstant() method in Java with Examples
- ZonedDateTime toString() method in Java with Examples
- ZonedDateTime minusSeconds() method in Java with Examples
- ZonedDateTime range() method in Java with Examples
- ZonedDateTime getZone() method in Java with Examples
- ZonedDateTime isSupported() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vaibhav2992



