Java Convert LocalDate to LocalDateTime
Tags: Java LocalDate Java LocalDateTime Java 8
In this Java core tutorial we learn how to convert a java.time.LocalDate object into java.time.LocalDateTime object in Java programming language.
Table of contents
- Convert LocalDate to LocalDateTime using atStartOfDay() method
- Convert LocalDate to LocalDateTime using atTime() method
Convert LocalDate to LocalDateTime using atStartOfDay() method
In Java we can use the LocalDate.atStartOfDay() method which combines a LocalDate value with the time of midnight to create a new LocalDateTime object as the following Java program.
ConvertLocalDateToLocalDateTimeExample1.java
import java.time.LocalDate;
import java.time.LocalDateTime;
public class ConvertLocalDateToLocalDateTimeExample1 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
LocalDateTime localDateTime = localDate.atStartOfDay();
System.out.println("LocalDate: " + localDate);
System.out.println("LocalDateTime: " + localDateTime);
}
}
LocalDate: 2022-04-18
LocalDateTime: 2022-04-18T00:00
Convert LocalDate to LocalDateTime using atTime() method
With a given LocalDate object we can use the LocalDate.atTime() method to combine with a specified LocalTime object or specified hour, minute, second values to create a new LocalDateTime object as below.
ConvertLocalDateToLocalDateTimeExample2.java
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class ConvertLocalDateToLocalDateTimeExample2 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
int hour = 0;
int minute = 0;
int second = 0;
int nanoOfSecond = 0;
LocalTime localTime = LocalTime.of(hour, minute);
LocalDateTime localDateTime1 = localDate.atTime(localTime);
LocalDateTime localDateTime2 = localDate.atTime(hour, minute);
LocalDateTime localDateTime3 = localDate.atTime(hour, minute, second);
LocalDateTime localDateTime4 = localDate.atTime(hour, minute, second, nanoOfSecond);
System.out.println("LocalDate: " + localDate);
System.out.println("localDateTime1: " + localDateTime1);
System.out.println("localDateTime2: " + localDateTime2);
System.out.println("localDateTime3: " + localDateTime3);
System.out.println("localDateTime4: " + localDateTime4);
}
}
LocalDate: 2022-04-18
localDateTime1: 2022-04-18T00:00
localDateTime2: 2022-04-18T00:00
localDateTime3: 2022-04-18T00:00
localDateTime4: 2022-04-18T00:00
Happy Coding 😊
Related Articles
Java Convert LocalDate to String
Java Convert LocalDate to OffsetDateTime
Java Convert LocalDate to Epoch Day
Java Convert LocalDate to ZonedDateTime
Java Convert LocalDate to Date
Java Convert LocalDate to Calendar
Java Convert Date to LocalDate