Java 8 – How to extract LocalDate and LocalTime from LocalDateTime ?

In this article, we will learn how to extract/get LocalDate & LocalTime from LocalDateTime using toLocalDate() and toLocalTime() methods respectively provided in Java 1.8 version

For LocalDate/LocalTime to LocalDateTime conversion, read

Get LocalDate/LocalTime from LocalDateTime :

  • LocalDateTime has below methods
    • toLocalDate() – gets the LocalDate part from the invoking LocalDateTime
    • toLocalTime() – gets the LocalTime part from the invoking LocalDateTime
  • Lets see an example for getting LocalDate & LocalTime from LocalDateTime in the below illustration

GetLocalDateAndLocalTimeFromLocalDateTime.java

package in.bench.resources.java8.localdatetime.examples;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class GetLocalDateAndLocalTimeFromLocalDateTime {

	public static void main(String[] args) {

		// 1. get current System Date/time
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Current System Date/time is :- \n" + localDateTime);


		// 2. get LocalDate from LocalDateTime
		LocalDate localDate = localDateTime.toLocalDate();
		System.out.println("\nLocalDate is :- \n" + localDate);


		// 3. get LocalTime from LocalDateTime
		LocalTime localTime = localDateTime.toLocalTime();
		System.out.print("\nLocalTime is :- \n" + localTime);
	}
}

Output:

Current System Date/time is :- 
2022-08-09T12:59:30.585626900

LocalDate is :- 
2022-08-09

LocalTime is :- 
12:59:30.585626900

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDateTime to java.util.Date and vice-versa ?
Java 8 – How to convert LocalDateTime to an Instant ?