Java LocalDate atTime Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the atTime() method in the LocalDate class in Java. The atTime() method is used to combine a date with a time to create a LocalDateTime object. The LocalDateTime object represents a date and time in the format of year, month, day, hour, minute, second and nanosecond.


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/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-117774{{"`Java LocalDate atTime Method`"}} java/packages_api -.-> lab-117774{{"`Java LocalDate atTime Method`"}} java/identifier -.-> lab-117774{{"`Java LocalDate atTime Method`"}} java/operators -.-> lab-117774{{"`Java LocalDate atTime Method`"}} java/output -.-> lab-117774{{"`Java LocalDate atTime Method`"}} java/strings -.-> lab-117774{{"`Java LocalDate atTime Method`"}} java/system_methods -.-> lab-117774{{"`Java LocalDate atTime Method`"}} end

Import required packages

In this step, we will import the LocalDate and LocalDateTime classes from the java.time package. In the code block below include the import statements.

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

Create a date using LocalDate class.

In this step, we will use the LocalDate class to create a date with a specified year, month, and day. In the code block below, write the code to create a date of February 20, 2018.

LocalDate localDate = LocalDate.of(2018,2,20);

Use the atTime() method to create LocalDateTime object

In this step, we will use the atTime() method to create a LocalDateTime object by combining our existing LocalDate object with a specified time. In the code block below create a LocalDateTime object at 12:25 by using the atTime() method on the LocalDate object we created in the previous step.

LocalDateTime localDateTime = localDate.atTime(12,25);

Display the LocalDateTime object

In this step, we will display our newly created LocalDateTime object to the console. In the code block below, write code to display the LocalDateTime object created in the previous step.

System.out.println("LocalDateTime object: "+localDateTime);

Create a date with a specified LocalTime object

In this step, we will create a date with a specified LocalTime object that specifies a full time. In the code block below, write code to create a LocalDate object for February 20, 2018, and a LocalTime object for 12:10:20.

LocalDate localDate2 = LocalDate.of(2018, 2, 20);
LocalTime localTime = LocalTime.parse("12:10:20");

Use the atTime() method to create LocalDateTime object

In this step, we will again use the atTime() method to create a LocalDateTime object by passing in our newly created LocalDate object and LocalTime object created in the previous step. In the code block below, write code to create a LocalDateTime object using atTime().

LocalDateTime localDateTime2 = localDate2.atTime(localTime);

Display the LocalDateTime object

In this step, we will display our newly created LocalDateTime object to the console. In the code block below, write code to display the LocalDateTime object created in the previous step.

System.out.println("Date with local time: "+localDateTime2);

Compile and run the program

To compile our program run the following command in your terminal

javac LocalDateAtTimeLab.java

To run the program, use:

java LocalDateAtTimeLab

Verify the output

The output of your program should be as follows:

LocalDateTime object: 2018-02-20T12:25
Date with local time: 2018-02-20T12:10:20

Summary

In this lab, you learned the atTime() method in the LocalDate class in Java. We learned how to combine a date and a time using atTime() to create a LocalDateTime object. We created a date using LocalDate class, used the atTime() method to create LocalDateTime object, and displayed the output to the console.

Other Java Tutorials you may like