How to Change the Date Format in a Java String

Avatar

By squashlabs, Last Updated: November 2, 2023

How to Change the Date Format in a Java String

To change the date format in a Java string, you can use the SimpleDateFormat class, which is part of the java.text package. This class allows you to specify a pattern for parsing and formatting dates.

Here are two possible approaches to changing the date format in a Java string:

Approach 1: Using SimpleDateFormat

1. Create an instance of the SimpleDateFormat class, passing the desired date format pattern as a string to the constructor. The pattern should contain a combination of letters and symbols that represent the different elements of the date.

Example:

SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");

2. Use the parse() method of the SimpleDateFormat class to parse the input string into a Date object. This method throws a ParseException if the input string does not match the specified format.

Example:

String inputString = "2021-07-01";
Date date = inputFormat.parse(inputString);

3. Create another instance of the SimpleDateFormat class with the desired output format pattern.

Example:

SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy");

4. Use the format() method of the SimpleDateFormat class to format the Date object into a string with the desired output format.

Example:

String outputString = outputFormat.format(date);

Here’s the complete code snippet:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatter {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
        String inputString = "2021-07-01";
        Date date = inputFormat.parse(inputString);
        SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy");
        String outputString = outputFormat.format(date);
        System.out.println(outputString);
    }
}

This will output: “01/07/2021”.

Related Article: How to Convert JsonString to JsonObject in Java

Approach 2: Using DateTimeFormatter (Java 8 and later)

Starting from Java 8, a new date and time API was introduced in the java.time package. This API provides the DateTimeFormatter class, which can be used to change the date format in a Java string.

1. Create an instance of the DateTimeFormatter class using the ofPattern() method, passing the desired date format pattern as a string.

Example:

DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

2. Use the parse() method of the DateTimeFormatter class to parse the input string into a LocalDate object. This method throws a DateTimeParseException if the input string does not match the specified format.

Example:

String inputString = "2021-07-01";
LocalDate date = LocalDate.parse(inputString, inputFormatter);

3. Create another instance of the DateTimeFormatter class with the desired output format pattern.

Example:

DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

4. Use the format() method of the DateTimeFormatter class to format the LocalDate object into a string with the desired output format.

Example:

String outputString = date.format(outputFormatter);

Here’s the complete code snippet:

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

public class DateFormatter {
    public static void main(String[] args) {
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String inputString = "2021-07-01";
        LocalDate date = LocalDate.parse(inputString, inputFormatter);
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        String outputString = date.format(outputFormatter);
        System.out.println(outputString);
    }
}

This will output: “01/07/2021”.

These are two possible approaches to change the date format in a Java string. Both approaches are valid and can be used depending on the version of Java you are working with and your specific requirements.

Related Article: How to Implement a Delay in Java Using java wait seconds

How to Convert a String to Date in Java

This article provides a step-by-step guide on how to convert a string to a date in Java. It covers two approaches: using SimpleDateFormat and using DateTimeFormatter... read more

How to Find the Max Value of an Integer in Java

This article provides a simple guide to finding the maximum integer value in Java. It covers various methods, including using the Integer.MAX_VALUE constant and the... read more

How to Use the JsonProperty in Java

This article dives into the purpose and usage of @JsonProperty in Java coding. It covers topics such as mapping Java fields to JSON properties, ignoring fields during... read more

How to Set the JAVA_HOME in Linux for All Users

Setting JAVA_HOME in Linux for all users can be achieved through two different methods. The first method involves setting JAVA_HOME in the /etc/environment file, while... read more

How to Compare Strings in Java

This article provides a simple guide on comparing strings in Java programming language. It covers topics such as using the equals() method and the compareTo() method.... read more

How to Generate Random Numbers in Java

Generating random numbers is a common task in Java programming. This article explores two approaches for generating random numbers using Java's java random class and the... read more