LocalDate to ZonedDateTime Conversion [2021]
To Convert LocalDate to ZonedDateTime we must add time and zoneId with LocalDate as ZonedDateTime has time and ZoneId added with the date.
LocalDate to ZonedDateTime conversion can be done using LocalDate.atStartOfDay() method which will first convert localdate to localdatetime and then localdatetime to ZonedDatetime using atZone(ZoneId zone) method on localdatetime instance.
Contents
Example 1: LocalDate to ZonedDateTime
In this example, we are going to create a localdate instance and then convert that to ZonedDateTime instance.
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class LocalDateToZonedDateTimeEx1 {
public static void main(String[] args) {
// Create LocalDate instance.
LocalDate date = LocalDate.now();
System.out.println("LocalDate : " + date);
// Create ZonedDateTime instance
ZonedDateTime time = date.atStartOfDay()
.atZone(ZoneId.systemDefault());
System.out.println("ZonedDateTime : " + time);
}
}
Output:
LocalDate : 2020-07-12
ZonedDateTime : 2020-07-12T00:00+05:30[Asia/Calcutta]
In this example, we are going to convert LocalDate instance to ZonedDateTime instance in UTC Format using ZoneId as UTC.
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class LocalDateToZonedDateTimeEx2 {
public static void main(String[] args) {
//Create LocalDate instance.
LocalDate date = LocalDate.now();
System.out.println("LocalDate : " + date);
// Create ZonedDateTime instance
ZonedDateTime time = date.atStartOfDay().atZone(ZoneId.of("UTC"));
System.out.println("ZonedDateTime : " + time);
}
}
Output:
LocalDate : 2020-07-12
ZonedDateTime : 2020-07-12T00:00Z[UTC]
Example 3: LocalDate to ZonedDateTime change timezone
In this example, we are going to convert LocalDate instance to ZonedDateTime instance and change the timezone of ZonedDateTime instance.
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class LocalDateToZonedDateTimeEx3 {
public static void main(String[] args) {
//Create LocalDate instance.
LocalDate date = LocalDate.now();
System.out.println("LocalDate : " + date);
// Create ZonedDateTime instance
ZonedDateTime time1 = date.atStartOfDay()
.atZone(ZoneId.of("UTC"));
System.out.println("ZonedDateTime with zone UTC : " + time1);
//Create ZonedDateTime instance with change timezone
ZonedDateTime time2 =
time1.withZoneSameInstant(ZoneId.of("US/Eastern"));
System.out.println("ZonedDateTime with zone Us/Eastern : " + time2);
}
}
Output:
LocalDate : 2020-07-12
ZonedDateTime with zone UTC : 2020-07-12T00:00Z[UTC]
ZonedDateTime with zone Us/Eastern : 2020-07-11T20:00-04:00[US/Eastern]
Example 4: string to ZonedDateTime
In this example, we are going to convert string formatted date to first localdate and then convert this localdate instance to ZonedDateTime instance.
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class StringToZonedDateTimeEx4 {
public static void main(String[] args) {
// string formatted date
String stringformattedtext = "2020-07-12";
LocalDate date = LocalDate.parse(stringformattedtext);
System.out.println("LocalDate : " + date);
// Create ZonedDateTime instance
ZonedDateTime time = date.atStartOfDay().atZone(ZoneId.systemDefault());
System.out.println("ZonedDateTime : " + time);
}
}
Output:
LocalDate : 2020-07-12
ZonedDateTime : 2020-07-12T00:00+05:30[Asia/Calcutta]
Conclusion:
In this tutorial, we have covered how we can convert
- localdate to zoneddatetime
- localdate to zoneddatetime with UTC timezone
- zoneddatetime with different timezone after changing the existing timezone
- string formatted localdate to zoneddatetime
Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.
Happy Learning!!!
More Examples you may like
Comments
Post a Comment
If you have any doubts, Please let me know.