In this article we are going to see the use of Java LocalDate class atTime() method with suitable examples.
Java LocalDate atTime( ) Method with Example
This java.time.LocalDate.atTime(LocalTime time)
method is used to combine a date with time to create LocalDateTime. It returns the local date-time formed by the specified date and time.
Syntax:
public LocalDateTime atTime(LocalTime time)
Where,
time
refers to the actual time to be combined with the date.
Let’s see the use of atTime() method.
- LocalDateTime atTime(int hour, int minute)
- LocalDateTime atTime(int hour, int minute, int second)
- LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond)
- LocalDateTime atTime(LocalTime time)
- OffsetDateTime atTime
Method-1: Java LocalDateTime class atTime(int hour, int minute)
The LocalDate.atTime(int hour, int minute)
method creates a LocalDateTime by combining the date with a specified time. Here hour and minute are passed as the parameter to the atTime()
method.
It returns the formed local date-time but if any value is out of range then it gives DateTimeException.
Syntax:
public LocalDateTime atTime(int hour, int minute)
Where,
hour
refers to the hour of the day, its value ranges from 0 to 23.minute
refers to the minute of the hour, its value ranges from 0 to 59.
Approach:
- Create an object of LocalDate class which will hold the parsed date.
- Then pass a time as parameter in (int hour, int minute) format to
atTime()
method which will add the specified time with the provided date and the final local date-time result will be hold by an object of LocalDateTime class. - Print the final 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 LocalDate date = LocalDate.parse("2017-02-03"); System.out.println("Specified date: "+date); //Create an object of LocalDateTime class //By using the object of LocalDate class and atTime() method create the local date-time LocalDateTime dateWithTime = date.atTime(1,20); //Print the result System.out.println("Final Date and Time: "+dateWithTime); } }
Output: Specified date: 2017-02-03 Final Date and Time: 2017-02-03T01:20
Method-2: Java LocalDateTime class atTime(int hour, int minute, int second)
The LocalDate.atTime(int hour, int minute, int second)
method creates a LocalDateTime by combining the date with a specified time. Here hour, minute and second are passed as the parameter to the atTime()
method.
It returns the formed local date-time but if any value is out of range then it gives DateTimeException.
Syntax:
public LocalDateTime atTime(int hour, int minute, int second)
Where,
hour
refers to the hour of the day, its value ranges from 0 to 23.minute
refers to the minute of the hour, its value ranges from 0 to 59.second
refers to the seconds of the minute, its value ranges from 0 to 59.
Approach:
- Create an object of LocalDate class which will hold the parsed date.
- Then pass a time as parameter in (int hour, int minute, int second) format to
atTime()
method which will add the specified time with the provided date and the final local date-time result will be hold by an object of LocalDateTime class. - Print the final 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 LocalDate date = LocalDate.parse("2017-02-03"); System.out.println("Specified date: "+date); //Create an object of LocalDateTime class //By using the object of LocalDate class and atTime() method create the local date-time LocalDateTime dateWithTime = date.atTime(1,20,25); //Print the result System.out.println("Final Date and Time: "+dateWithTime); } }
Output: Specified date: 2017-02-03 Final Date and Time: 2017-02-03T01:20:25
Method-3: Java LocalDateTime class atTime(int hour, int minute, int second, int nanoOfSecond)
The LocalDate.atTime(int hour, int minute, int second, int nanoOfSecond)
method creates a LocalDateTime by combining the date with a specified time. Here hour, minute, second and Nano second are passed as the parameter to the atTime()
method.
It returns the formed local date-time but if any value is out of range then it gives DateTimeException.
Syntax:
public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond)
Where,
hour
refers to the hour of the day, its value ranges from 0 to 23.minute
refers to the minute of the hour, its value ranges from 0 to 59.second
refers to the seconds of the minute, its value ranges from 0 to 59.nanoOfSecond
refers to the Nano seconds of the second, its value ranges from 0 to 999999999.
Approach:
- Create an object of LocalDate class which will hold the parsed date.
- Then pass a time as parameter in (int hour, int minute, int second, int nanoOfSecond) format to
atTime()
method which will add the specified time with the provided date and the final local date-time result will be hold by an object of LocalDateTime class. - Print the final 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 LocalDate date = LocalDate.parse("2017-02-03"); System.out.println("Specified date: "+date); //Create an object of LocalDateTime class //By using the object of LocalDate class and atTime() method create the local date-time LocalDateTime dateWithTime = date.atTime(1,20,25,345); //Print the result System.out.println("Final Date and Time: "+dateWithTime); } }
Output: Specified date: 2017-02-03 Final Date and Time: 2017-02-03T01:20:25.000000345
Method-4: Java LocalDateTime class atTime(LocalTime time)
The LocalDate.atTime(LocalTime time)
method creates a LocalDateTime by combining the date with a specified time. Here specific time is passed as the parameter to the atTime()
method.
It returns the formed local date-time but if any value is out of range then it gives DateTimeException.
Syntax:
public LocalDateTime atTime(LocalTime time)
Where,
time
refers to the time in LocalTime format.
Approach:
- Create an object of LocalDate class which will hold the parsed date.
- Create an object of LocalTime class which will hold the parsed time.
- Then pass a time as parameter in (LocalTime time) format to
atTime()
method which will add the specified time with the provided date and the final local date-time result will be hold by an object of LocalDateTime class. - Print the final result.
Program:
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; public class Main { public static void main(String[] args) { //Create an object of LocalDate class and assign a date to it LocalDate date = LocalDate.parse("2017-02-03"); System.out.println("Specified date: "+date); //Create an object of LocalTime class and pass a parse value of time LocalTime time = LocalTime.parse("09:20:45"); //Create an object of LocalDateTime class //By using the object of LocalDate class and atTime() method create the local date-time LocalDateTime dateWithTime = date.atTime(time ); //Print the result System.out.println("Final Date and Time: "+dateWithTime); } }
Output: Specified date: 2017-02-03 Final Date and Time: 2017-02-03T09:20:45
Method-5: OffsetDateTime atTime(OffsetTime time)
The OffsetDateTime atTime(OffsetTime time)
method creates a OffsetDateTime by combining the date with a offset time. Here specific time is passed as the parameter to the atTime()
method.
It returns the formed local date-time but if any value is out of range then it gives DateTimeException.
Syntax:
public OffsetDateTime atTime(OffsetTime time)
Where,
time
refers to the time in OffsetTime format.
Approach:
- Create an object of LocalDate class which will hold the parsed date.
- Create an object of OffsetTime class which will hold the offset time.
- Then pass a time as parameter in (OffsetTime time) format to
atTime()
method which will add the specified time with the provided date and the final local date-time result will be hold by an object of LocalDateTime class. - Print the final result.
Program:
import java.time.LocalDate; import java.time.OffsetDateTime; import java.time.OffsetTime; public class Main { public static void main(String[] args) { //Create an object of LocalDate class and assign a date to it LocalDate date = LocalDate.parse("2017-02-03"); System.out.println("Specified date: "+date); //Create an object of OffsetTime class OffsetTime time = OffsetTime.now(); //Create an object of LocalDateTime class //By using the object of OffsetDateTime class and atTime() method create the local date-time OffsetDateTime dateWithTime = date.atTime(time ); //Print the result System.out.println("Final Date and Time: "+dateWithTime); } }
Output: Specified date: 2017-02-03 Final Date and Time: 2017-02-03T14:17:35.166418Z
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.