ZonedDateTime form() method in Java with Examples
The from() method of ZonedDateTime class in Java is used to get instance of ZonedDateTime from TemporalAccessor object passed as parameter. A TemporalAccessor represents an arbitrary set of date and time information and this method helps to get an instant of ZonedDateTime based on the specified TemporalAccessor object.
Syntax:
public static ZonedDateTime
from(TemporalAccessor temporal)
Parameters: This method accepts a single parameter temporal which represents the temporal object to convert. This is a mandatory parameter and it should not be NULL.
Return value: This method returns a the zoned date-time.
Exception: This method throws a DateTimeException if unable to convert to an ZonedDateTime.
Below programs illustrate the from() method:
Program 1:
// Java program to demonstrate // ZonedDateTime.from() method import java.time.*; public class GFG { public static void main(String[] args) { // create a ZonedDateTime object ZonedDateTime zonedDT = ZonedDateTime.now(); // create a ZonedDateTime object using // from() method ZonedDateTime result = ZonedDateTime.from(zonedDT); // print result System.out.println("ZonedDateTime: " + result); } } |
ZonedDateTime: 2018-12-12T19:03:06.445Z[Etc/UTC]
Program 2:
// Java program to demonstrate // ZonedDateTime.from() method import java.time.*; public class GFG { public static void main(String[] args) { // create a OffsetDateTime object OffsetDateTime offset = OffsetDateTime.now(); // create a ZonedDateTime object using // from() method ZonedDateTime result = ZonedDateTime.from(offset); // print result System.out.println("ZonedDateTime: " + result); } } |
ZonedDateTime: 2018-12-12T19:03:09.523Z
Recommended Posts:
- ZonedDateTime with() Method in Java with Examples
- ZonedDateTime get() method in Java with Examples
- ZonedDateTime plus() method in Java with Examples
- ZonedDateTime of() Method in Java with Examples
- ZonedDateTime now() Method in Java with Examples
- ZonedDateTime until() Method in Java with Examples
- ZonedDateTime minusWeeks() method in Java with Examples
- ZonedDateTime toLocalDate() method in Java with Examples
- ZonedDateTime parse() method in Java with Examples
- ZonedDateTime minusYears() method in Java with Examples
- ZonedDateTime plusYears() method in Java with Examples
- ZonedDateTime plusWeeks() method in Java with Examples
- ZonedDateTime ofStrict() Method in Java with Examples
- ZonedDateTime plusDays() method in Java with Examples
- ZonedDateTime getMinute() 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 : Akanksha_Rai



