Skip to content
Related Articles

Related Articles

Improve Article

LocalDateTime from() method in Java with Examples

  • Last Updated : 30 Nov, 2018

The from() method of LocalDateTime class is used to obtain an instance of LocalDateTime from the temporal object passed as the parameter.

Syntax:

public static LocalDateTime
    from(TemporalAccessor temporal)

Parameter: This method accepts a parameter temporal which specifies the temporal object to be converted to the LocalDateTime instance. It should not be null.

Returns: The function returns the LocalDateTime which is the converted LocalDateTime from the temporal object.

Exceptions: The program throws DateTimeException which is thrown if it is unable to convert the given date to a LocalDateTime.



Below programs illustrate the LocalDateTime.from() method:

Program 1:




// Program to illustrate the from() method
  
import java.util.*;
import java.time.*;
import java.time.temporal.ChronoField;
  
public class GfG {
    public static void main(String[] args)
    {
        LocalDateTime date
            = LocalDateTime
                  .from(ZonedDateTime.now());
  
        System.out.println(date);
    }
}
Output:
2018-11-30T07:53:17.939

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#from(java.time.temporal.TemporalAccessor)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :