Signup/Sign In

Java LocalDate atTime() Method With Examples

The atTime() method is used to combine this date with a time. It takes two parameters and returns a LocalDateTime.

We can use all the possible valid combinations of date and time to create a date. The syntax of the method is given below.

Syntax

public LocalDateTime atTime(int hour, int minute)

Parameters

hour - The hour-of-day to use, from 0 to 23

minute - The minute-of-hour to use, from 0 to 59

Returns

The local date-time formed from this date and the specified time.

Example:

We are creating a date using the LocalDate class and then combining time with it to using the atTime() method. This method returns date with time, see the below example.

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

public class Demo {  
	public static void main(String[] args){  
		
		// Take a date
		LocalDate localDate1 = LocalDate.of(2018, 2, 20);
		// Displaying date
		System.out.println("Date is : "+localDate1);
		// using atTime() method
		LocalDateTime localDateTime = localDate1.atTime(12,25);
		System.out.println("Date with local time: "+localDateTime);
	}
} 


Date is : 2018-02-20
Date with local time: 2018-02-20T12:25

Example:

The atTime() method has an overloaded method that takes an instance of LocalTime class. It is useful when we want to create a date with full time. See the below example.

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

public class Demo {  
	public static void main(String[] args){  
		
		// Take a date
		LocalDate localDate1 = LocalDate.of(2018, 2, 20);
		// Displaying date
		System.out.println("Date is : "+localDate1);
		// Getting a full time
		LocalTime time = LocalTime.parse("12:10:20");
		// using atTime() method
		LocalDateTime localDateTime = localDate1.atTime(time);
		System.out.println("Date with local time: "+localDateTime);
	}
}


Date is : 2018-02-20
Date with local time: 2018-02-20T12:10:20

Live Example:

Try with a live example, execute the code instantly with our power Online Java Compiler.



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.