Java – Convert Date from one format to another

In Java, you may need to convert dates from one format to another, whether it be for display purposes or to store the dates in a database. In this post, we’ll look at how to use the “SimpleDateFormat” class to convert dates from one format to another.

Converting Dates from one Format to Another in Java

Step 1 : Creating a SimpleDateFormat Object

The first step in converting dates is to create a “SimpleDateFormat” object. You’ll use this object to specify the input format and the output format.

Here’s an example of how to create a SimpleDateFormat object:

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

In this example, the “inputFormat” object is set to the input format of “dd-MM-yyyy”, and the “outputFormat” object is set to the output format of “yyyy-MM-dd”.

Step 2 : Parsing a Date

The next step is to parse the input date and convert it to a Date object. This is done using the parse() method of the SimpleDateFormat class.

Here’s an example of how to parse a date:

Date date = inputFormat.parse("12-06-2018");

In this example, the date “12-06-2018” is parsed using the inputFormat object and stored in the “date” variable.

Step 3 : Formatting a Date

Finally, you can format the date and convert it to the desired output format. This is done using the format() method of the SimpleDateFormat class.

Here’s an example of how to format a date:

String output = outputFormat.format(date);

Complete Example : Convert Date from one format to another using SimpleDateFormat

We can convert a java.util.Date from one format to another using SimpleDateFormat.

For example, if we want to convert a Date to MM/dd/yyyy, we can do the same using :


new SimpleDateFormat("MM-dd-yyyy").format(myDate)

Here is a complete example, that converts an UTC formatted date into MM/dd/yyyy and yyyy-MM-dd formats.

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

public class SomeProgram {

  public static void main(String[] args) {
    String utcDateStr = "2018-07-31T12:00:00Z";
    try {
      convert(utcDateStr);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

  public static void convert(String dateString) throws ParseException {
    System.out.println("Given date is " + dateString);

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    Date date = sdf.parse(dateString);

    System.out.println("MM/dd/yyyy formatted date : " + new SimpleDateFormat("MM/dd/yyyy").format(date));
    System.out.println("yyyy-MM-dd formatted date : " + new SimpleDateFormat("yyyy-MM-dd").format(date));

  }
}

Output :

Given date is 2018-07-31T12:00:00Z
MM/dd/yyyy formatted date : 07/31/2018
yyyy-MM-dd formatted date : 2018-07-31
 
Similarly, we can convert a date string from MM/dd/yyyy to yyyy-MM-dd using :

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

public class SomeProgram {

  public static void main(String[] args) {
    String utcDateStr = "07/05/2018";
    try {
      convert(utcDateStr);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

  public static void convert(String dateString) throws ParseException {
    System.out.println("Given date is " + dateString);

    DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    Date date = sdf.parse(dateString);
    System.out.println("yyyy-MM-dd formatted date : " + new SimpleDateFormat("yyyy-MM-dd").format(date));

  }

}

Output :

Date in MM/dd/yyyy format is 07/05/2018
yyyy-MM-dd formatted date : 2018-07-05
 

Convert a LocalDate or LocalDateTime from one format to another using DateTimeFormatter in Java 8

Java 8 introduced a DateTimeFormatter class that can be used to convert a LocalDate or LocalDateTime from one format to another.

For example, if we want to convert a LocalDate to MM/dd/yyyy, we can do the same using :


DateTimeFormatter.ofPattern("MM/dd/yyyy").format(date)

The following example converts LocalDateTime from UTC(yyyy-MM-dd’T’HH:mm:ss’Z’) format to MM/dd/yyyy and yyyy-MM-dd formats.

public static void convert(String dateString) throws ParseException {
  System.out.println("Given date is " + dateString);
  LocalDateTime date = LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"));

  System.out.println("MM/dd/yyyy formatted date : " + DateTimeFormatter.ofPattern("MM/dd/yyyy").format(date));
  System.out.println("yyyy-MM-dd formatted date : " + DateTimeFormatter.ofPattern("yyyy-MM-dd").format(date));

}

Output :

Given date is 2018-07-31T12:00:00Z
MM/dd/yyyy formatted date : 07/31/2018
yyyy-MM-dd formatted date : 2018-07-31

 

© 2018 – 2023, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags