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:ss a");
t = ft.parse(date); //Date is Sep 16, 2015 10:34:23 AM and of type string.
ft.applyPattern("MM.dd");
but I am getting exception at t = ft.parse(date);
Please help
ISO 8601. This format is defined by the sensible practical standard, ISO 8601. The T separates the date portion from the time-of-day portion. The Z on the end means UTC (that is, an offset-from-UTC of zero hours-minutes-seconds).
dd – two-digit day of the month, e.g. 02. ddd – three-letter abbreviation for day of the week, e.g. Fri. dddd – day of the week spelled out in full, e.g. Friday.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With