How to format date from one format to another format in JAVA?

public static String formateDateFromstring(String inputFormat, String outputFormat, String inputDate){

    Date parsed = null;
    String outputDate = "";

    SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
    SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());

    try {
        parsed = df_input.parse(inputDate);
        outputDate = df_output.format(parsed);

    } catch (ParseException e) {
    }

    return outputDate;

}

Usage:
String inputdate="01-JAN-2021";
String convertedDate = formateDateFromstring(
        "dd-MMM-yyyy",
        "dd/MM/yyyy",
        inputdate;
Output: 01/01/2021

Add a Comment

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