Search code examples
javasimpledateformat

Java Date TIME Format AM/PM Configuration


I am facing problems some while formatting the date:

Date : 11/06/2020 04:14:20

Date Format:dd/MM/yyyy hh:mm:ss a

Exception:

java.text.ParseException: Unparseable date: "11/06/2020 04:14:20"

Following is the code

Blockquote

public String getFormatDate(String inputDate) {
        String strDate = "";        
        try {           
            DateFormat outputFormat = new SimpleDateFormat("MMMM dd, yyyy hh:mm:ss a");
            DateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
            Date date1 = inputFormat.parse(inputDate);
            strDate = outputFormat.format(date1);           
        }catch( Exception exe) {
            exe.printStackTrace();
            logger.error( "[ERROR] getFormatDate:. ", exe );
        } 
        return strDate;     
    }

Blockquote

Any help would be greatly appeciated.


Solution

  • You can check this code you have to pass the am/pm part too with the date string value as your format is expecting that.

    //String date = "11/06/2020 04:14:20";
    String date = "11/06/2020 04:14:20 am";
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
    

    https://ideone.com/3nibwJ