Java LocalDate atStartOfDay Method With Time Zone

JavaJavaBeginner
Practice Now

Introduction

Java atStartOfDay(ZoneId) method is used to combine start time (midnight time) with the specified date based on the time-zone. This method returns zoned date-time rather than local date-time. In this lab, you will learn how to use the atStartOfDay() method with time zone in Java.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/date -.-> lab-117772{{"`Java LocalDate atStartOfDay Method With Time Zone`"}} java/packages_api -.-> lab-117772{{"`Java LocalDate atStartOfDay Method With Time Zone`"}} java/identifier -.-> lab-117772{{"`Java LocalDate atStartOfDay Method With Time Zone`"}} java/comments -.-> lab-117772{{"`Java LocalDate atStartOfDay Method With Time Zone`"}} java/operators -.-> lab-117772{{"`Java LocalDate atStartOfDay Method With Time Zone`"}} java/output -.-> lab-117772{{"`Java LocalDate atStartOfDay Method With Time Zone`"}} java/strings -.-> lab-117772{{"`Java LocalDate atStartOfDay Method With Time Zone`"}} java/system_methods -.-> lab-117772{{"`Java LocalDate atStartOfDay Method With Time Zone`"}} end

Create a Java file

Firstly, navigate to the ~/project directory and create a new java file named LocalDateStartOfDay.java using the command below:

cd ~/project
touch LocalDateStartOfDay.java

Then, open the LocalDateStartOfDay.java file with your favorite text editor.

Import necessary packages

In this step, we will import the necessary packages to work with the LocalDate, Month, ZoneId and ZonedDateTime classes.

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

Get zoned date-time from a start date

In this step, we will create a LocalDate object with a specified date, then we will use the atStartOfDay() method to get the zoned date-time from this date with a specific time zone.

// Getting a date
LocalDate localDate = LocalDate.of(2022, Month.APRIL, 15);

// Getting start time of the date
ZonedDateTime localDateTime = localDate.atStartOfDay(ZoneId.of("Asia/Kolkata"));

// Displaying date and time of start of the date
System.out.println("Date with start time: \n"+localDateTime);

Get zoned date-time from a start date with system default time zone

In this step, we will use the atStartOfDay() method with the ZoneId.systemDefault() method to get the zoned date-time from the specified date with the default time zone of the system.

// Getting start time of the date with system default time zone
ZonedDateTime localDateTime = localDate.atStartOfDay(ZoneId.systemDefault());

// Displaying date and time of start of the date
System.out.println("Date with start time: \n"+localDateTime);

Compile and run the Java program

In this step, we will compile and run the Java program using the following command in the terminal:

javac LocalDateStartOfDay.java && java LocalDateStartOfDay

View the output

The output should be as below, giving you the date with start time in the timezone specified.

Date with start time:
2022-04-15T00:00+05:30[Asia/Kolkata]
Date with start time:
2022-04-15T00:00+02:00[Europe/Paris]

Summary

In this lab, you have learnt how to use the Java atStartOfDay(ZoneId) method to combine the start time with the specified date based on the time zone and get the zoned date-time of the specified date with start time. You have also learnt how to get the zoned date-time of the specified date with start time and the default time zone of the system.

Other Java Tutorials you may like