I want to format date from \"MMM dd, yyyy HH:mm:ss a\" to \"MM.dd\". I have following code
SimpleDateFormat ft = new SimpleDateFormat (\"MMM dd, yyyy hh:mm:s
SimpleDateFormat sdf = new SimpleDateFormat("MM.dd", Locale.US);
System.out.println("Formatted Date: " + sdf.format(date));
Three possible explanations:
Sep
as a month namet
is the wrong type (e.g. java.sql.Date
instead of java.util.Date
, or some other type altogether), or is not declared.You should include details of the exception in your question to figure out which it is, but here's a working example using basically your own code, with the addition of a specific Locale
.
SimpleDateFormat ft = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US);
java.util.Date t=ft.parse("Sep 16, 2015 10:34:23 AM");
ft.applyPattern("MM.dd");
System.out.println(ft.format(t));
output:
09.16