In this article we are going to see the use of Java LocalDate class atStartOfDay( ) method with suitable examples.
Java LocalDate atStartOfDay( ) Method with Example
This java.time.LocalDate.atStartOfDay() method is used to combine the date with the time of midnight to create a LocalDate Time. It returns the local date’s time of midnight at the start of the day.
Syntax:
public LocalDateTime atStartOfDay()
Let’s see the use of atStartOfDay( ) method with 2 different format.
Method-1: without parameters
Approach:
- Create an object of LocalDate class which will hold the parsed date.
- By using that LocalDate class object call
atStartOfDay()method and assign the result to an object of LocaldateTime class. - Then print the final LocalDateTime as result.
Program:
import java.time.LocalDate;
import java.time.LocalDateTime;
public class Main
{
public static void main(String[] args)
{
//Create an object of LocalDate class and assign a date to it
//here it parses the local date
LocalDate date = LocalDate.parse("2022-05-10");
System.out.println("Specific date: "+date);
//Create an object of LocalDateTime
//call the atStartOfDay() method by using object of LocalDate class
LocalDateTime startTime = date.atStartOfDay();
System.out.println("Start Time: "+startTime);
}
}
Output: Specific date: 2022-05-10 Start Time: 2022-05-10T00:00
Method-2: with parameters
Approach:
- Create an object of LocalDate class which will hold the parsed date.
- By using that LocalDate class object call
atStartOfDay()method and by passingZoneId.systemDefault()as parameter and assign the result to an object ofZoneddateTimeclass. - Here we pass the
ZoneIdwhich is already defined by system so that it will take a particular zone and represent that zone’s start time. - Then print the final ZonedDateTime as result.
Program:
import java.time.*;
public class Main
{
public static void main(String[] args)
{
//Create an object of LocalDate class and assign a date to it
//here it parses the local date
LocalDate date = LocalDate.parse("2022-05-10");
System.out.println("Specific date: "+date);
//Create an object of LocalDateTime
//call atStartOfDay() method by passing the ZoneId.systemDefault() as parameter
ZonedDateTime startTime = date.atStartOfDay(ZoneId.systemDefault());
System.out.println("Start Time: "+startTime);
}
}
Output: Specific date: 2022-05-10 Start Time: 2022-05-10T00:00Z[GMT]
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.