Android change date format

Unbeatable Date: "02/20/2014"

I have a date "02/20/2014" in String format and you want to change its format to "MMMM dd, yyyy", but it gives an 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();
}
+3
source share
4 answers

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();        }
+3
source

You do not change the date format. You give a 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();
}

To change the format and have a new date, you need to use the command format

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

your string is in the format dd/MM/yyyy

 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();
}
+2
source

Do it like

   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();  
    }
+2
source

All Articles