Reputation: 2619
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();
}
Upvotes: 3
Views: 273
Reputation: 12042
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();
}
Upvotes: 2
Reputation: 157437
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();
}
Upvotes: 2
Reputation: 6711
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);
Upvotes: 3
Reputation: 3508
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(); }
Upvotes: 3