Signup/Sign In

Java LocalDate atStartOfDay() Method with Examples

Java atStartOfDay() method belongs to the LocalDate class. This method is used to combine date with the time of midnight at start of this date.

This method does not take any argument and returns a LocalDateTime formed from the date at the time of midnight. Syntax of the method is given below.

Syntax

public LocalDateTime atStartOfDay()

Returns:

It returns the local date-time of midnight at the start of this date.

Now, let's take some examples to understand the functionality of this method.

Example: Combine Date With Start Time

Here we are getting the current date using Java now() method and then calling atStartOfDay() method to combine the date with start time of the day.

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

public class Demo {  
	public static void main(String[] args){  
		
		// Getting date of today
		LocalDate localDate = LocalDate.now();
		// Displaying date
		System.out.println("Today's Date: \n"+localDate);
		// Getting todays start time
		LocalDateTime localDateTime = localDate.atStartOfDay();
		// Displaying date and time of start of today
		System.out.println("Today's Date with time: \n"+localDateTime);
	}
}  


Today's Date:
2020-05-28
Today's Date with time:
2020-05-28T00:00

Example: Combine Specific Date With Start Time

If we want to get start time (midnight time) of any specific date then simply create a date using of() method of LocalDate class and then combine the time. Here we are using atStartOfDay() method that returns the local date time.

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

public class Demo {  
	public static void main(String[] args){  
		
		// Getting a date
		LocalDate localDate = LocalDate.of(2018, Month.JANUARY, 18);
		// Displaying date
		System.out.println("Date: \n"+localDate);
		// Getting start time of the date
		LocalDateTime localDateTime = localDate.atStartOfDay();
		// Displaying date and time of start of the date
		System.out.println("Date with start time: \n"+localDateTime);
	}
} 


Date:
2018-01-18
Date with start time:
2018-01-18T00:00

Live Example:

Here, you can test the live code example. You can execute the example, even can edit and write your own examples to test the Java code.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.