In this article, we will learn how to convert LocalDate to LocalDateTime using different methods of LocalDate provided in Java 1.8 version
For LocalDateTime to LocalDate conversion, read Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?
Convert LocalDate to LocalDateTime :
- LocalDate provides different methods to convert LocalDate to LocalDateTime, those are listed below
- atStartOfDay() – Combines invoking LocalDate with the time of midnight to create a
LocalDateTime
at the start of this date - atTime(int hour, int minute) – Combines invoking LocalDate with a time (hour, minute) provided to create a
LocalDateTime
- atTime(int hour, int minute, int second) – Combines invoking LocalDate with a time (hour, minute, second) provided to create a
LocalDateTime
- atTime(int hour, int minute, int second, int nanoOfSecond) – Combines invoking LocalDate with a time (hour, minute, second, nanosecond) provided to create a
LocalDateTime
- atTime(LocalTime localTime) – Combines invoking LocalDate with LocalTime provided to create a
LocalDateTime
- First, convert LocalDate to ZonedDateTime and then to LocalDateTime using toLocalDateTime() method of ZonedDateTime
- atStartOfDay() – Combines invoking LocalDate with the time of midnight to create a
- Lets see an example for each of the above listed methods
1. Using atStartOfDay() method :
- Invoke atStartOfDay() method on LocalDate which will convert LocalDate to LocalDateTime at the start of the Day or Midnight with hour & minute parts set to 00
ConvertLocalDateToLocalDateTime.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class ConvertLocalDateToLocalDateTime {
public static void main(String[] args) {
// 1. get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is :- \n" + localDate);
// 2. convert LocalDate to LocalDateTime using atStartOfDay()
LocalDateTime localDateTime = localDate.atStartOfDay();
System.out.print("\nConversion of LocalDate to LocalDateTime "
+ "using atStartOfDay() is :- \n"
+ localDateTime);
}
}
Output:
Current System Date is :-
2022-08-01
Conversion of LocalDate to LocalDateTime using atStartOfDay() is :-
2022-08-01T00:00
2. Using atTime(hour, minute) method :
- This method converts LocalDate to LocalDateTime with provided values for hour/minute
ConvertLocalDateToLocalDateTime2.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class ConvertLocalDateToLocalDateTime2 {
public static void main(String[] args) {
// 1. get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is :- \n" + localDate);
// 2. convert LocalDate to LocalDateTime using atTime(int hour, int minute)
LocalDateTime localDateTime = localDate.atTime(10, 45);
System.out.print("\nConversion of LocalDate to LocalDateTime "
+ "using atTime(int hour, int minute) is :- \n"
+ localDateTime);
}
}
Output:
Current System Date is :-
2022-08-01
Conversion of LocalDate to LocalDateTime using atTime(int hour, int minute) is :-
2022-08-01T10:45
3. Using atTime(hour, minute, second) method :
- This method converts LocalDate to LocalDateTime with provided values for hour/minute/second
ConvertLocalDateToLocalDateTime3.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class ConvertLocalDateToLocalDateTime3 {
public static void main(String[] args) {
// 1. get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is :- \n" + localDate);
// 2. convert LocalDate to LocalDateTime using atTime(int hour, int minute, int second)
LocalDateTime localDateTime = localDate.atTime(10, 45, 53);
System.out.print("\nConversion of LocalDate to LocalDateTime "
+ "using atTime(int hour, int minute, int second) is :- \n"
+ localDateTime);
}
}
Output:
Current System Date is :-
2022-08-01
Conversion of LocalDate to LocalDateTime using atTime(int hour, int minute, int second) is :-
2022-08-01T10:45:53
4. Using atTime(hour, minute, second, nano) method :
- This method converts LocalDate to LocalDateTime with provided values for hour/minute/second/nano
ConvertLocalDateToLocalDateTime4.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class ConvertLocalDateToLocalDateTime4 {
public static void main(String[] args) {
// 1. get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is :- \n" + localDate);
// 2. convert LocalDate to LocalDateTime
// using atTime(int hour, int minute, int second, int nanoOfSecond)
LocalDateTime localDateTime = localDate.atTime(10, 45, 53, 977);
System.out.print("\nUsing atTime(int hour, int minute, int second, int nanoOfSecond) is :- \n"
+ localDateTime);
}
}
Output:
Current System Date is :-
2022-08-01
Using atTime(int hour, int minute, int second, int nanoOfSecond) is :-
2022-08-01T10:45:53.000000977
5. Using atTime(LocalTime) method :
- This method converts LocalDate to LocalDateTime with provided LocalTime value
ConvertLocalDateToLocalDateTime5.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class ConvertLocalDateToLocalDateTime5 {
public static void main(String[] args) {
// 1. get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is :- \n" + localDate);
// 2. convert LocalDate to LocalDateTime using atTime(LocalTime localTime)
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = localDate.atTime(localTime);
System.out.print("\nConversion of LocalDate to LocalDateTime "
+ "using atTime(LocalTime localTime) is :- \n"
+ localDateTime);
}
}
Output:
Current System Date is :-
2022-08-01
Conversion of LocalDate to LocalDateTime using atTime(LocalTime localTime) is :-
2022-08-01T14:07:53.012164200
6. LocalDate -> ZonedDateTime -> LocalDateTime :
- First, convert LocalDate to ZonedDateTime using atStartOfDay() method passing ZoneId as argument
- Then, convert ZonedDateTime to LocalDateTime using toLocalDateTime() method which returns LocalDateTime with hour/minute values set to 00
ConvertLocalDateToLocalDateTime6.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ConvertLocalDateToLocalDateTime6 {
public static void main(String[] args) {
// 1. get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is :- \n" + localDate);
// 2. first convert LocalDate to ZonedDateTime using atStartOfDay(ZoneId)
ZoneId zoneId = ZoneId.of("Europe/Paris");
ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId);
// 3. convert ZonedDateTime to LocalDateTime using toLocalDateTime()
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
System.out.print("\nConversion of LocalDate to LocalDateTime "
+ "via ZonedDateTime is :- \n"
+ localDateTime);
}
}
Output:
Current System Date is :-
2022-08-01
Conversion of LocalDate to LocalDateTime via ZonedDateTime is :-
2022-08-01T00:00
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!