Using Java LocalDate Now Clock

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the now(Clock) method to generate local date based on zone across the world. You will learn the syntax, parameters, and returns of the method. This lab assumes you have a basic understanding of Java programming language.


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/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") 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/class_methods -.-> lab-117824{{"`Using Java LocalDate Now Clock`"}} java/date -.-> lab-117824{{"`Using Java LocalDate Now Clock`"}} java/modifiers -.-> lab-117824{{"`Using Java LocalDate Now Clock`"}} java/identifier -.-> lab-117824{{"`Using Java LocalDate Now Clock`"}} java/arrays -.-> lab-117824{{"`Using Java LocalDate Now Clock`"}} java/data_types -.-> lab-117824{{"`Using Java LocalDate Now Clock`"}} java/operators -.-> lab-117824{{"`Using Java LocalDate Now Clock`"}} java/output -.-> lab-117824{{"`Using Java LocalDate Now Clock`"}} java/strings -.-> lab-117824{{"`Using Java LocalDate Now Clock`"}} java/system_methods -.-> lab-117824{{"`Using Java LocalDate Now Clock`"}} end

Import the necessary packages

Import the java.time package in your code by adding the following line at the beginning of the DateDemo.java file.

import java.time.*;

Create a main() method

Create a main() method in your code as shown below.

public static void main(String[] args) {

}

Generate current date based on UTC time-zone

Use systemUTC() method of Clock class to generate current date based on UTC time-zone as shown below.

LocalDate localDate = LocalDate.now(Clock.systemUTC());
System.out.println("Current date based on UTC time-zone: " + localDate);

Generate current date based on system default clock

Use systemDefaultZone() method of Clock class to generate the current date based on the system default clock as shown below.

LocalDate localDate1 = LocalDate.now(Clock.systemDefaultZone());
System.out.println("Current date based on System Default Zone: " + localDate1);

Compile and run the code

Save the DateDemo.java file and open the terminal. Navigate to the ~/project directory and run the following commands to compile and run the code.

javac DateDemo.java
java DateDemo

Output

The output of the program should be displayed on the terminal as shown below.

Current date based on UTC time-zone: 2022-11-08
Current date based on System Default Zone: 2022-11-08

Summary

In this lab, we have learned how to use Java LocalDate now(Clock) method to get the current date and time based on a specified clock. We have seen examples of generating the current date based on UTC time-zone and system default clock. Now you can use this method to get the current date and time based on your desired clock.

Other Java Tutorials you may like