ZonedDateTime form() method in Java with Examples
Last Updated :
13 Apr, 2023
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 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
import java.time.*;
public class GFG {
public static void main(String[] args)
{
ZonedDateTime zonedDT
= ZonedDateTime.now();
ZonedDateTime result = ZonedDateTime.from(zonedDT);
System.out.println("ZonedDateTime: "
+ result);
}
}
|
Output:
ZonedDateTime: 2018-12-12T19:03:06.445Z[Etc/UTC]
Program 2:
Java
import java.time.*;
public class GFG {
public static void main(String[] args)
{
OffsetDateTime offset
= OffsetDateTime.now();
ZonedDateTime result = ZonedDateTime.from(offset);
System.out.println("ZonedDateTime: "
+ result);
}
}
|
Output:
ZonedDateTime: 2018-12-12T19:03:09.523Z
Reference: https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#from(java.time.temporal.TemporalAccessor)
Share your thoughts in the comments
Please Login to comment...