LocalDateTime from() method in Java


An instance of a LocalDateTime object can be obtained from a Temporal object using the from() method in the LocalDateTime class in Java. This method requires a single parameter i.e. the Temporal object and it returns the LocalDateTime object that is obtained from the Temporal object.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.*;
public class Main {
   public static void main(String[] args) {
      LocalDateTime ldt = LocalDateTime.from(ZonedDateTime.now());
      System.out.println("The LocalDateTime is: " + ldt);
   }
}

Output

The LocalDateTime is: 2019-02-18T09:55:05.489

Now let us understand the above program.

The instance of the LocalDateTime object is obtained from the Temporal object using the from() method and then it is displayed. A code snippet that demonstrates this is as follows −

LocalDateTime ldt = LocalDateTime.from(ZonedDateTime.now());
System.out.println("The LocalDateTime is: " + ldt);

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

37 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements