$show=/label

Java LocalDate atStartOfDay() Example

SHARE:

A quick guide to atStartOfDay() method in LocalDate class from java 8 api. Example programs to convert LocalDate to LocalDateTime and ZonedDateTime.

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.


COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java LocalDate atStartOfDay() Example
Java LocalDate atStartOfDay() Example
A quick guide to atStartOfDay() method in LocalDate class from java 8 api. Example programs to convert LocalDate to LocalDateTime and ZonedDateTime.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjG9zRctyC2EWp8ksRzpiPrXbwulXIcN8lfhxlKAi5ouUfKzSj3ftRWmRL1nGwtTgQQQ54XBgj4DLUm27DaegpHqWzP8m57CGYpMRbT1YuOgDuIWO3qogjNXSSmrRXt1bCPMGBG_MqHh9I/w640-h480/Java+LocalDate+atStartOfDay%2528%2529+Example.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjG9zRctyC2EWp8ksRzpiPrXbwulXIcN8lfhxlKAi5ouUfKzSj3ftRWmRL1nGwtTgQQQ54XBgj4DLUm27DaegpHqWzP8m57CGYpMRbT1YuOgDuIWO3qogjNXSSmrRXt1bCPMGBG_MqHh9I/s72-w640-c-h480/Java+LocalDate+atStartOfDay%2528%2529+Example.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/12/java-localdate-atstartofday.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/12/java-localdate-atstartofday.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content