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 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
// 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
// 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



