Pages

Saturday, January 23, 2021

Java LocalDate atStartOfDay() Example

1. Overview

In this tutorial, We'll learn how to use LocalDate.atStartOfDay() method in java 8. This is the newly added method in Date Time API in java 8.

atStartOfDay() method does appends the might night date with time part as 00:00 to LocalDate.

This is an overloaded method and available in two versions as below. 

public LocalDateTime atStartOfDay()

public ZonedDateTime atStartOfDay(ZoneId zone)


public LocalDateTime atStartOfDay(): This method appends the mid night to the LocalDate and returns it as LocalDateTime object with time default values as zero's.

public ZonedDateTime atStartOfDay(ZoneId zone): This method takes ZoneId instance for time zone value and converts the LocalDate to ZonedDateTime by adding the time part as zero's.

Let us explore the examples on these two methods with examples.

Java LocalDate atStartOfDay() Example



But, you need to remember one core point is that LocalDate does not store the time part but to convert it to LocalDateTime, we need to add time part. This adding the time part is done with atStartOfDay() method.


2. Java 8 LocalDate.atStartOfDay() - LocalDate to LocalDateTime


Understand the example program using atStartOfDay() method with zero parameters to convert localdate to LocalDateTime object to mid night and adding time units with default values as 0.

package com.javaprogramto.java8.dates.localdate;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Example for public LocalDateTime atStartOfDay() method in LocalDate class in
 * java 8.
 * 
 * @author JavaProgramTo.com
 *
 */

public class LocalDateAtStartOfDayExamples {

	public static void main(String[] args) {
		// Example 1

		// creating LocalDate object
		LocalDate localDate1 = LocalDate.now();

		// converting LocalDate object to LocalDateTime object
		LocalDateTime localDateTime1 = localDate1.atStartOfDay();

		// printing the LocalDate, LocalDateTime objects
		System.out.println("localDate1 : " + localDate1);
		System.out.println("localDateTime1 : " + localDateTime1);

		// Example 2

		// creating LocalDate object
		LocalDate localDate2 = LocalDate.of(2023, 02, 02);

		// converting LocalDate object to LocalDateTime object
		LocalDateTime localDateTime2 = localDate2.atStartOfDay();

		// printing the LocalDate, LocalDateTime objects
		System.out.println("localDate2 : " + localDate2);
		System.out.println("localDateTime2 : " + localDateTime2);
	}
}

Output:
localDate1 : 2020-12-07
localDateTime1 : 2020-12-07T00:00
localDate2 : 2023-02-02
localDateTime2 : 2023-02-02T00:00

3. Java 8 LocalDate.atStartOfDay(ZoneId z) - LocalDate to ZonedDateTime


Next, example on how to convert LocalDate to ZonedLocalDate in java 8 with atStartOfDay() method that takes ZoneId as argument.

This method also works by adding time units as zero but it converts the date part as per the given timezone id.

In the below example, we have tried with the system timezone, Singapore and EST time zone dates.

package com.javaprogramto.java8.dates.localdate;

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

/**
 * Example for public ZonedDateTime atStartOfDay(ZoneId zoneid) method in
 * LocalDate class in java 8.
 * 
 * @author JavaProgramTo.com
 *
 */

public class LocalDateAtStartOfDayZoneIdExamples {

	public static void main(String[] args) {
		// Example 1 - IST

		// creating LocalDate object
		LocalDate localDate1 = LocalDate.now();

		// Getting the default timezone from system.
		ZoneId defaultId = ZoneId.systemDefault();

		// converting LocalDate object to ZonedDateTime object using timezone.
		ZonedDateTime zonedDateTime1 = localDate1.atStartOfDay(defaultId);

		// printing the LocalDate, ZonedDateTime objects
		System.out.println("localDate1 : " + localDate1);
		System.out.println("zonedDateTime1 : " + zonedDateTime1);

		// Example 2 - EST

		// creating LocalDate object
		LocalDate localDate2 = LocalDate.of(2023, 02, 02);

		// Getting the EST timezone from system.
		ZoneId timezoneESTId = ZoneId.of("US/Eastern");

		// converting LocalDate object to ZonedDateTime object using est zone id;
		ZonedDateTime zonedDateTime2 = localDate2.atStartOfDay(timezoneESTId);

		// printing the LocalDate, ZonedDateTime objects
		System.out.println("localDate2 : " + localDate2);
		System.out.println("zonedDateTime2 : " + zonedDateTime2);

		// Example 2 - Singapore

		// creating LocalDate object
		LocalDate localDate3 = LocalDate.of(2023, 02, 02);

		// Getting the Asia/Singapore timezone from system.
		ZoneId timezoneSingaporeId = ZoneId.of("Asia/Singapore");

		// converting LocalDate object to ZonedDateTime object using Asia/Singapore zone id;
		ZonedDateTime zonedDateTime3 = localDate3.atStartOfDay(timezoneSingaporeId);

		// printing the LocalDate, ZonedDateTime objects
		System.out.println("localDate3 : " + localDate3);
		System.out.println("zonedDateTime3 : " + zonedDateTime3);
	}
}
Output:
localDate1 : 2020-12-07
zonedDateTime1 : 2020-12-07T00:00+05:30[Asia/Kolkata]
localDate2 : 2023-02-02
zonedDateTime2 : 2023-02-02T00:00-05:00[US/Eastern]
localDate3 : 2023-02-02
zonedDateTime3 : 2023-02-02T00:00+08:00[Asia/Singapore]
From the output, We can see that timezone is added in the returned ZonedDateTime object and time units are 00:00 values.


4. Conclusion


In this article, we've seen how to use LocalDate.atStartOfDay() method with examples to convert LocalDate object to LocalDateTime and ZoneDateTime objects in java 8.


No comments:

Post a Comment

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