Convert LocalDateTime to LocalDate in Java

This example shows how to convert LocalDateTime to LocalDate instance with an example.

Convert LocalDateTime to LocalDate Example

LocalDateTime class provides below API to convert from LocalDateTime to LocalDate in Java.
  • LocalDate toLocalDate() - Gets the LocalDate part of this date-time.
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Program to demonstrate LocalDateTime Class APIs.
 * @author javaguides.net
 *
 */
public class LocalDateTimeExample {

    public static void main(String[] args) {
        convertLocalDateTimeToLocalDate();
    }

    private static void convertLocalDateTimeToLocalDate() {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(dateTime);

        LocalDate localDate = dateTime.toLocalDate();
        System.out.println(localDate);
    }
}
Output:
2018-08-10T18:16:35.802
2018-08-10
To convert LocalDateTime to LocalDate instance, use toLocalDate() method. It returns a LocalDate with the same year, month and day as given date-time.

Comments