ChronoLocalDateTime from() method in Java with Examples


The from() method is a static factory method in the ChronoLocalDateTime interface in Java. It is used to obtain an instance of ChronoLocalDateTime from a temporal object. The temporal object must have the required fields to create a ChronoLocalDateTime instance.

Syntax:

public static ChronoLocalDateTime<?> from(TemporalAccessor temporal)

Parameters: - temporal: the temporal object to convert, not null

Returns: - a ChronoLocalDateTime instance, not null

Example 1: Using from() method with LocalDateTime

LocalDateTime localDateTime = LocalDateTime.of(2021, 10, 31, 12, 0, 0);
ChronoLocalDateTime<?> chronoLocalDateTime = ChronoLocalDateTime.from(localDateTime);
System.out.println(chronoLocalDateTime);

Output:

2021-10-31T12:00

Example 2: Using from() method with ZonedDateTime

ZonedDateTime zonedDateTime = ZonedDateTime.of(2021, 10, 31, 12, 0, 0, 0, ZoneId.of("Asia/Kolkata"));
ChronoLocalDateTime<?> chronoLocalDateTime = ChronoLocalDateTime.from(zonedDateTime);
System.out.println(chronoLocalDateTime);

Output:

2021-10-31T12:00+05:30[Asia/Kolkata]

Example 3: Using from() method with OffsetDateTime

OffsetDateTime offsetDateTime = OffsetDateTime.of(2021, 10, 31, 12, 0, 0, 0, ZoneOffset.of("+05:30"));
ChronoLocalDateTime<?> chronoLocalDateTime = ChronoLocalDateTime.from(offsetDateTime);
System.out.println(chronoLocalDateTime);

Output:

2021-10-31T12:00+05:30


About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.