Java LocalDate from() Method with Examples

In this article we are going to see the use of Java LocalDate class from() method with suitable examples.

Java LocalDate from() Method with Examples

This java.time.LocalDate.from(TemporalAccessor temporal) method is used to obtain an instance of LocalDate from a temporal object to create a LocalDate Time. It returns the local date.

Syntax:

public static LocalDate from(TemporalAccessor temporal)

Where,

  • temporal refers to the Temporal object that to be concverted.

Let’s see an program to understand it more clearly.

Approach:

  • Create an object of localDate class.
  • By using that LocalDate class object call from() method and use ZonedDateTime.now() method to get the value.
  • Print the final LocalDate as result.

Program:

import java.time.LocalDate;
import java.time.ZonedDateTime;
public class Main
{
    public static void main(String[] args)
    {
        //create an object of localDate class
        //pass ZonedDateTime.now() in from() method.
      	LocalDate date = LocalDate.from(ZonedDateTime.now());
        //Print the result
      	System.out.println("Local date: "+date);  
   }
}
Output:

Local date: 2022-05-25

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.