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:
- 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/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.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 !!