Java 8 – Convert between LocalDateTime and LocalDate

Learn to convert from LocalDate to LocalDateTime and from LocalDateTime to LocalDate in Java 8.

To restate, LocalDate represent a calendar date without time and timezone. LocalDateTime only lacks the timezone information.

LocalDateTime = LocalDate + time information

1. LocalDate to LocalDateTime conversion

To convert a LocalDate instance to LocalDateTime instance, we need to add only the timepart in it. For this we can use either of given 5 methods of LocalDate class.

  • LocalDateTime atStartOfDay()
  • LocalDateTime atTime(LocalTime time)
  • LocalDateTime atTime(int hour, int minutes)
  • LocalDateTime atTime(int hour, int minutes, int seconds)
  • LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond)

The method atStartOfDay() returns a LocalDateTime formed from given date at the time of midnight, 00:00, at the start of given date.

For all other methods, we provide the specific time in method arguments.

  • hour – the hour-of-day to use, from 0 to 23
  • minute – the minute-of-hour to use, from 0 to 59
  • second – the second-of-minute to represent, from 0 to 59
  • nanoOfSecond – the nano-of-second to represent, from 0 to 999,999,999

Java Example

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

public class Main {

    public static void main(String[] args) 
    {

        LocalDate localDate = LocalDate.parse("2019-01-04");
        
        LocalDateTime localDateTime1 = localDate.atStartOfDay();
        System.out.println(localDateTime1);
        
        LocalDateTime localDateTime2 = localDate.atTime(LocalTime.now());
        System.out.println(localDateTime2);
        
        LocalDateTime localDateTime3 = localDate.atTime(04, 30, 56);
        System.out.println(localDateTime3);
    }
}

Program output.

2019-01-04T00:00
2019-01-04T18:31:21.936
2019-01-04T04:30:56

2. LocalDateTime to LocalDate conversion

To convert LocalDateTime to LocalDate instance, use toLocalDate() method. It returns a LocalDate with the same year, month and day as given date-time.

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

public class Main {
    public static void main(String[] args) {

        LocalDateTime localDateTime = LocalDateTime.now();
        
        LocalDate localDate = localDateTime.toLocalDate();
        
        System.out.println(localDate);
    }
}

Program output.

2019-04-01

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Java 8 – Convert between LocalDateTime and LocalDate”

  1. The java.time API is an improvement, but still has design issues compared to the best API currently being rolled out in the C++ standard for example. The documentation about the toLocalDate method of the LocalDateTime class is unfortunately too vague or missleading. Given a LocalDateTime which shows as 2019-10-25T00:00, and whose internal fields match those printed values, the toLocalDate method returns a LocalDate that’s 2019-10-24 on my system. I’ll have to resort to reading the actual source code implementation to have any clue what to do.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.