Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, December 7, 2020

Java Convert LocalDate to ZonedDateTime

1. Overview

In this tutorial, We'll learn how to convert the LocalDate to ZonedDateTime in java 8.

LocalDate is having only date units without timezone and ZonedDateTime object contains date, time units with timezone information. But, when we want to convert LocalDate to ZonedDateTime, we have to supply the remaining time units and timezone values and add to the LocalDate object.

This is solved using a simple method atStartOfDay() from LocalDate class with ZoneId information as an argument.

Let us explore how to convert String to ZonedDateTime and LocalDate to ZonedDateTime object.

Java Convert LocalDate to ZonedDateTime


2. Java 8 Convert String to ZonedDateTime Object


In the below example, we are taking the string as input date in format "yyyy-MM-dd" and pass to the LocalDate.parse() method and then covert it to the ZonedDateTime object.

package com.javaprogramto.java8.dates.localdate;

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

/**
 * Example to convert String to ZonedDateTime.
 * 
 * @author JavaProgramTo.com
 *
 */
public class StringToZonedDateTimeExample {

	public static void main(String[] args) {

		// date in string format
		String date = "2022-03-02";

		// Converting String to LocalDate.
		LocalDate localDate = LocalDate.parse(date);

		// Creating timezone
		ZoneId zoneid = ZoneId.systemDefault();

		// LocalDate to zoneddatetime
		ZonedDateTime zonedDateTimeFromString = localDate.atStartOfDay(zoneid);

		System.out.println("String date : " + date);
		System.out.println("ZonedDateTime : " + zonedDateTimeFromString);
	}
}
Output:

String date : 2022-03-02
ZonedDateTime : 2022-03-02T00:00+05:30[Asia/Kolkata]

3. Java 8 Convert LocalDate to ZonedDateTime Object


Next, convert the LocalDate object into ZonedDateObject with PST America timezone.

Use atStartOfDay() method with PST timezone is passed using ZoneId.of() method.

package com.javaprogramto.java8.dates.localdate;

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

/**
 * Example to convert LocalDate to ZonedDateTime with PST zone.
 * 
 * @author JavaProgramTo.com
 *
 */
public class LocalDateToZonedDateTimePSTExample {

	public static void main(String[] args) {

		// Creating LocalDate object
		LocalDate localDate = LocalDate.now();

		// Creating timezone with PST zone id. We can pass here any timezone id supported by java.
		ZoneId zoneid = ZoneId.of("US/Pacific");

		// LocalDate to PST zoneddatetime
		ZonedDateTime zonedDateTimeInPST = localDate.atStartOfDay(zoneid);

		System.out.println("localDate : " + localDate);
		System.out.println("ZonedDateTime in PST : " + zonedDateTimeInPST);
	}
}

Output:

localDate : 2020-12-07
ZonedDateTime in PST : 2020-12-07T00:00-08:00[US/Pacific]

4. Conclusion


In this article, We've seen the examples on how to convert LocalDate to ZonedDateTime in java 8 api.



No comments:

Post a Comment

Please do not add any spam links in the comments section.