Signup/Sign In
LAST UPDATED: JUNE 15, 2020

Java LocalDate from() Method With Examples

This method is used to get localdate from a TemporalAccessor instance. TemporalAccessor is an interface, any object that inherits it can be used with the from() method. The from() method converts the temporal object into a localdate. The syntax of this method is given below.

Syntax

public static LocalDate from(TemporalAccessor temporal)

Parameters:

temporal - an object to convert into localdate.

Returns:

It returns a localdate.

Example: date from() method

In this example, we are using from() method to get localdate from a TemporalAccessor object. Notice we used of() method to create date and stored into TemporalAccessor object which further is used with from() method to get localdate.

import java.time.LocalDate;
import java.time.temporal.TemporalAccessor;

public class DateDemo {
public static void main(String[] args){  
		
		// Take a date
	    TemporalAccessor date = LocalDate.of(2012,06,02);
		// Displaying date
		System.out.println("Date : "+date);
		// from method
		LocalDate date2 = LocalDate.from(date);
		System.out.println(date2);

	}
}


Date : 2012-06-02
2012-06-02

Example:

In this example, we used ZonedDateTime to get the current date and assigned to temporalaccessor object that further is used with from() method to get localdate.

import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAccessor;

public class DateDemo {
public static void main(String[] args){  
		
		// Take a date
	    TemporalAccessor date = ZonedDateTime.now();
		// Displaying date
		System.out.println("Date : "+date);
		// from method
		LocalDate date2 = LocalDate.from(date);
		System.out.println(date2);

	}
}


Date : 2020-06-01T15:28:21.841342+05:30[Asia/Kolkata]
2020-06-01

Live Example:

Try with a live example, execute the code instantly with our powerful Online Java Compiler.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.