Java LocalDate Format Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to use the Java LocalDate format method to format date and time in Java programming language. This method takes an argument of DateTimeFormatter to format the date and returns a date string.

Set up your Java programming environment

Before we start working on the Java LocalDate format method, we need to set up a Java programming environment. In this step, we’ll download and install the Java Development Kit (JDK).

  1. Open the terminal and update the package list:

    sudo apt update
  2. Install OpenJDK with the following command:

    sudo apt install default-jdk
  3. Verify that Java is installed by running the following command:

    java -version

    If Java is installed, the output will look similar to this:

    openjdk version "11.0.9.1" 2020-11-04
    OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.18.04.3)
    OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.18.04.3, mixed mode, sharing)

Create a Java class file

In this step, we’ll create a Java class file in which we’ll write our Java code.

  1. Open the terminal and create a file named DateDemo.java in the project directory:

    cd ~/project
    touch DateDemo.java
  2. Open the DateDemo.java file in a text editor.

Import the required packages

In this step, we’ll import the required packages for the Java LocalDate format method.

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

Format a date

In this step, we’ll format a date into a specific format. We’ll use the ofPattern() method to specify the format pattern and then call the format() method on it.

LocalDate date = LocalDate.parse("2018-02-03");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String localDate = formatter.format(date);
System.out.println("Date : " + date);
System.out.println("Date2 : " + localDate);

Format the current date

In this step, we’ll format the current system date by using the now() method to get the current date and then call the format() method.

LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY");
String formattedDate = formatter.format(currentDate);
System.out.println("Current Date : " + currentDate);
System.out.println("Formatted Date : " + formattedDate);

Compile and run the program

  1. Save the DateDemo.java file.

  2. To compile the program, run the following command in the terminal:

    javac DateDemo.java
  3. To run the program, run the following command:

    java DateDemo
  4. The output should be similar to the following:

    Date : 2018-02-03
    Date2 : 03/02/2018
    Current Date : 2021-06-29
    Formatted Date : 29/06/2021

Modify the format pattern

In this step, we’ll modify the format pattern to display the date in a different format.

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

Compile and run the program

  1. Save the DateDemo.java file.

  2. To compile the program, run the following command in the terminal:

    javac DateDemo.java
  3. To run the program, run the following command:

    java DateDemo
  4. The output should be similar to the following:

    Date : 2018-02-03
    Date2 : 03/Feb/2018
    Current Date : 2021-06-29
    Formatted Date : 29/Jun/2021

Summary

In this lab, we learned how to use the Java LocalDate format method to format date and time in Java programming language. We also learned how to modify the format pattern. The DateTimeFormatter class provides various methods to format the date and time in the desired format.

Other Java Tutorials you may like