Java 8 – How to convert LocalDate to ZonedDateTime ?

In this article, we will learn how to convert LocalDate to ZonedDateTime using atStartOfDay() method of LocalDate provided in Java 1.8 version

For ZonedDateTime to LocalDate conversion, read Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?

Convert LocalDate to ZonedDateTime :

  • LocalDate has a method atStartOfDay() which takes ZoneId as argument and returns ZonedDateTime
    • atStartOfDay() – Returns a zoned date-time from invoking LocalDate at the earliest valid time according to the rules in the time-zone
  • Using this method, it is very easy to convert LocalDate to ZonedDateTime
  • After conversion, ZonedDateTime have Date part same as that of LocalDate and Time part will contain only hour/minute values set to 00
  • Lets see an example for conversion of LocalDate to ZonedDateTime in the below illustration

ConvertLocalDateToZonedDateTime.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ConvertLocalDateToZonedDateTime {

	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. get system default zone
		ZoneId zoneId = ZoneId.systemDefault();
		System.out.println("\nDefault System Zone is :- \n" + zoneId);


		// 3. convert LocalDate to ZonedDateTime using atStartOfDay(ZoneId zone)
		ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId);
		System.out.print("\nConversion of LocalDate to ZonedDateTime is :- \n"
				+ zonedDateTime);
	}
}

Output:

Current System Date is :- 
2022-08-01

Default System Zone is :- 
Asia/Calcutta

Conversion of LocalDate to ZonedDateTime is :- 
2022-08-01T00:00+05:30[Asia/Calcutta]

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalDate to an OffsetDateTime ?
Java 8 – How to convert LocalDate to LocalDateTime ?