Learn Current Local Date

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about Java LocalDate now() method which is used to get the current local date. It returns the default system date based on the locale.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) 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/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117822{{"`Learn Current Local Date`"}} java/classes_objects -.-> lab-117822{{"`Learn Current Local Date`"}} java/class_methods -.-> lab-117822{{"`Learn Current Local Date`"}} java/date -.-> lab-117822{{"`Learn Current Local Date`"}} java/modifiers -.-> lab-117822{{"`Learn Current Local Date`"}} java/oop -.-> lab-117822{{"`Learn Current Local Date`"}} java/packages_api -.-> lab-117822{{"`Learn Current Local Date`"}} java/identifier -.-> lab-117822{{"`Learn Current Local Date`"}} java/arrays -.-> lab-117822{{"`Learn Current Local Date`"}} java/comments -.-> lab-117822{{"`Learn Current Local Date`"}} java/data_types -.-> lab-117822{{"`Learn Current Local Date`"}} java/operators -.-> lab-117822{{"`Learn Current Local Date`"}} java/output -.-> lab-117822{{"`Learn Current Local Date`"}} java/strings -.-> lab-117822{{"`Learn Current Local Date`"}} java/variables -.-> lab-117822{{"`Learn Current Local Date`"}} java/string_methods -.-> lab-117822{{"`Learn Current Local Date`"}} java/system_methods -.-> lab-117822{{"`Learn Current Local Date`"}} end

Import required packages

The java.time package contains the LocalDate class, which we need to use in our program. We also need to import java.time.format.DateTimeFormatter class to format the LocalDate output.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

Define the main method

Let's define the main() method in the program.

public class LocalDateDemo {
    public static void main(String[] args){
        // Code goes here
    }
}

Get current local date

In order to get the current local date using now() method, create a LocalDate object as shown below.

LocalDate currentDate = LocalDate.now();

Print the current date

To print the current date using now() method, we can print the currentDate object created in the previous step.

System.out.println(currentDate);

Format the current date

If we want to format the output of now() method, we can create a DateTimeFormatter object with the required format and use the format() method to apply the format to the currentDate object.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formattedDate = currentDate.format(formatter);
System.out.println("Formatted date: " + formattedDate);

Compile and run the program

To compile the program, open the terminal and navigate to the ~/project directory. Then, enter the following command:

javac LocalDateDemo.java

After successful compilation, run the program with the following command:

java LocalDateDemo

Output

The program will output the current date in the default format and the formatted date in the desired format.

2020-11-13
Formatted date: 13/11/2020

Summary

In this lab, you have learned how to use Java LocalDate now() method to get the current local date. You also learned how to format the output of the now() method using DateTimeFormatter class in Java.

Other Java Tutorials you may like