Format date from "MMM dd, yyyy HH: mm: ss a" to "MM.dd

I want to format the date from "MMM dd, yyyy HH: mm: ss a" to "MM.dd". I have the 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 get an exception in t = ft.parse(date);

Please, help

+4
source share
2 answers

Three possible explanations:

  • Your default locale is incompatible with the input date - for example. he cannot understand Sephow the name of the month
  • something is wrong with the input line, or
  • t- this is the wrong type (for example, java.sql.Dateinstead of java.util.Dateor generally some other type) or not declared.

, , , 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));

:

09.16
+6
SimpleDateFormat sdf = new SimpleDateFormat("MM.dd", Locale.US);
System.out.println("Formatted Date: " + sdf.format(date));
+1

Source: https://habr.com/ru/post/1607682/


All Articles