How to change the format of date from dd/mm/yyyy to mm/dd/yyyy in Java

In this demo, we are going to learn how to change the format of date from ‘dd/mm/yyyy’ to ‘mm/dd/yyyy’ using Java programming language.

Java program to change the format of date from ‘dd/mm/yyyy’ to ‘mm/dd/yyyy’:

We make use of the class java.text.SimpleDateFormat. It provides convenient methods to format and parse dates. It also checks for the validity of the dates. Here, formatting is converting from Date to String and Parsing is converting from String to Date.
We make use of the class java.util.Date to create Date objects, store the parsed dates and format them.
We make use of the classes java.util.regex.Matcher and java.util.regex.Pattern to compile the regular expression and find the respective matches.

Steps:

  1. Ask the user to input the date in dd/mm/yyyy format.
  2. Check if the date entered is in the right format. Use the concept of regular expressions in Java which checks if ‘dd’ ranges from 1 to 31, ‘mm’ ranges from 1 to 12, and ‘yyyy’ is a 4 digit number.
    Regular Expression = “^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[0-2])/[0-9]{4}$”
  3. Next check the validity of the date, that is, if the date exists in the calendar or not, by setting the setLenient() method to false. For example, 31/02/2000 is an invalid date.
  4. If the date entered is in the wrong format or invalid, ask the user to enter the correct date.
  5. Once the date entered by the user is valid and in the correct format, parse the date and change its format to ‘mm/dd/yyyy’ .
  6. After changing the format of the given date, print it.

Java Code:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.SimpleDateFormat;
import java.util.Date;

public class data_format {
  
  public static void main(String args[]){
    
    Scanner s = new Scanner(System.in);
    
    //Setting the format as dd/mm/yyyy to parse the date entered by the user. 
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
    formatter.setLenient(false); //To check for the validity of the date. 
        
    //Regular expression to check the format of the entered date entered by the user. 
    String regex = "^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[0-2])/[0-9]{4}$";
    Pattern pattern = Pattern.compile(regex);
    
    System.out.println("Enter the date in dd/mm/yyyy format:");
    
    while(true){
      String EnterDate = s.next();
      Matcher match = pattern.matcher(EnterDate); 
      
      //match.find() returns true if the EnterDate is in the correct format.
      if(match.find()) {
        try { 
            //Parsing the date entered by the user and checking for its validity. 
            Date date = formatter.parse(EnterDate); 
            //Formatting the date to mm/dd/yyyy.
            String FromattedDate = new SimpleDateFormat("MM/dd/yyyy").format(date); 
            System.out.println("After changing the format of date from dd/mm/yyyy to mm/dd/yyyy: " + FromattedDate); 
            break;
        } 
        catch (Exception e) {
            //This block is entered when there is a parsing exception.
            System.out.println("Enter a valid date: ");
        }
      }
      else
        System.out.println("Enter a valid date in the fomrat dd/mm/yyyy: ");
    }	
  }
}

Output:

Enter the date in dd/mm/yyyy format:
13/04/22
Enter a valid date in the fomrat dd/mm/yyyy:
31/02/2000
Enter a valid date: 
16/05/2000
After changing the format of date from dd/mm/yyyy to mm/dd/yyyy: 05/16/2000

In the above output, the first date entered is not in the dd/mm/yyyy format, and the second date entered is not valid. The third date entered is valid and in the right format, and is converted to the mm/dd/yyyy format.

Leave a Reply

Your email address will not be published. Required fields are marked *