Question

Unparseable date: "20/02/2014"

I have date "20/02/2014" in String format and want to change it's format to "MMMM dd,yyyy" but it gives error.

Here is the code:

  try {
    SimpleDateFormat df = new SimpleDateFormat("MMMM dd,yyyy");
     Date date = df.parse("20/02/2014"); 
    System.out.println(date); 
} catch (ParseException e) {  

    e.printStackTrace();  
} catch (java.text.ParseException e) {
    e.printStackTrace();
}
Was it helpful?

Solution

try this code :

try {
          String str = "20/02/2014";
          SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
          SimpleDateFormat sdf2 = new SimpleDateFormat("MMMM dd,yyyy");

          System.out.println("Formatted Date : "+sdf2.format(sdf1.parse(str)));       } catch (Exception e) {
      e.printStackTrace();        }

OTHER TIPS

You're not changing the date format. You are passing date in this format dd/mm/yyyy:

try {
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
     Date date = df.parse("20/02/2014"); 
    System.out.println(date); 
} catch (ParseException e) {  

    e.printStackTrace();  
} catch (java.text.ParseException e) {
    e.printStackTrace();
}

In order to change the format and have a new Date, you need to use the format command

SimpleDateFormat  sdf = new SimpleDateFormat("MMMM dd,yyyy");
date = sdf.format(date); 

your String is in the dd/MM/yyyy format

 try {
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat df = new SimpleDateFormat("MMMM dd,yyyy");
     String date = formatter.parse("20/02/2014"); 
    Date date2 = df.format(date);
    System.out.println(date); 
} catch (ParseException e) {  

    e.printStackTrace();  
} catch (java.text.ParseException e) {
    e.printStackTrace();
}

Do like this

   try {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date date = df.parse("20/02/2014"); 
        System.out.println(sdf.format(date)); 
    } catch (ParseException e) {  

        e.printStackTrace();  
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top