How to Change Date Format in Java

Rashmi Patidar Feb 02, 2024
How to Change Date Format in Java

There are various options available to convert a date string in date format. Below are mentioned ways in which can bring out the desired results. Let us understand the variety of ways from the below code block.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class StringToDateFormat {
  public static void main(String[] args) throws ParseException {
    System.out.print("Way1: ");
    SimpleDateFormat dt = new SimpleDateFormat("yyyyy-MM-dd");
    System.out.print(dt.parse("2021-11-05") + "\n");

    System.out.print("Way2: ");
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss", Locale.ENGLISH);
    System.out.print(formatter.parse("21/JAN/2021 21:35:56") + "\n");

    System.out.print("Way3: ");
    DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("EEE, d MMM yyyy", Locale.ENGLISH);
    System.out.print(LocalDate.parse("Wed, 5 May 2021", formatter1) + "\n");

    System.out.print("Way4: ");
    System.out.print(LocalDate.parse("2021-05-31") + "\n");
  }
}

In Way1, an instance of the SimpleDateFormat class gets created. It takes a pattern value of the format in which the Date string gets entered. So in this way, we have entered a date in yyyy-MM-dd format. The instantiation also throws some exceptions as NullPointerException and IllegalArgumentException if the parameter is null or illegal. Now with the recently created a formatter object, we initialize a parse method. The method takes the date string as an input value and returns the Date datatype after parsing. It throws ParseException when the given date string and formatter do not match, or internally the date string does not parse.

In Way2, again SimpleDateFormat class is used to create a format that is supposed to enter. But now, an overridden constructor of SimpleDateFormat gets called. The first parameter is the format/pattern of the Date string. Another is Locale that defines a specified geographical region or area. Note: All Locales are not allowed in the method. Now, check the dd/MMM/yyyy HH:mm: ss pattern that had a month in an mmm format. The format implies shorthand of the months is acceptable in the mmm form. Additionally, it can take hours, minutes, and seconds in the format string.

In Way3, the use of the DateTimeFormat class is made to format and print date-time objects. The method ofPattern is used to prepare a formatter of the desired pattern. Now static method of the LocalDate class is called to parse the date. The method is parse that takes the text to parse and DateTimeFormatter for specifying the format of input date text. The method returns the LocalDate instance and is not null. It throws DateTimeParseException when the text cannot get parsed. The format can additionally take the day name. The EEE abbreviation denotes the same in the formatter.

In Way4, directly the parse method that is a static factory method of the LocalDate class gets called. This time no formatter instance or a pattern gets defined in any way. Now the function takes the input date string in the yyyy-MM-dd form. The specified date string must always represent a valid date and gets converted using the DateTimeFormatter.ISO_LOCAL_DATE format. The method throws the exception DateTimeParseException when the text cannot get parsed.

Below is the output of the code to convert the date string into the Date form.

Way1: Fri Nov 05 00:00:00 IST 2021
Way2: Thu Jan 21 21:35:56 IST 2021
Way3: 2021-05-05
Way4: 2021-05-31
Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.

LinkedIn

Related Article - Java Date