Java 8 – How to convert LocalDate to LocalDateTime ?

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
    1. atStartOfDay() – Combines invoking LocalDate with the time of midnight to create a LocalDateTime at the start of this date
    2. atTime(int hour, int minute) – Combines invoking LocalDate with a time (hour, minute) provided to createLocalDateTime
    3. atTime(int hour, int minute, int second) – Combines invoking LocalDate with a time (hour, minute, second) provided to createLocalDateTime
    4. atTime(int hour, int minute, int second, int nanoOfSecond) – Combines invoking LocalDate with a time (hour, minute, second, nanosecond) provided to create LocalDateTime
    5. atTime(LocalTime localTime) – Combines invoking LocalDate with LocalTime provided to create LocalDateTime
    6. First, convert LocalDate to ZonedDateTime and then to LocalDateTime using toLocalDateTime() method of ZonedDateTime
  • 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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDate to ZonedDateTime ?
Java 8 – How to find difference between two LocalDate instances using Period ?