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.
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.
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).
Open the terminal and update the package list:
sudo apt update
Install OpenJDK with the following command:
sudo apt install default-jdk
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)
In this step, we’ll create a Java class file in which we’ll write our Java code.
Open the terminal and create a file named DateDemo.java
in the project directory:
cd ~/project
touch DateDemo.java
Open the DateDemo.java
file in a text editor.
In this step, we’ll import the required packages for the Java LocalDate format method.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
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);
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);
Save the DateDemo.java
file.
To compile the program, run the following command in the terminal:
javac DateDemo.java
To run the program, run the following command:
java DateDemo
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
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);
Save the DateDemo.java
file.
To compile the program, run the following command in the terminal:
javac DateDemo.java
To run the program, run the following command:
java DateDemo
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
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.