Invalid month name with SimpleDateFormat

I have a problem with this small piece of code

SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); String str = "2010-03-13 01:01:22"; Date date = sf.parse(str); SimpleDateFormat f = new SimpleDateFormat("d MMM yyyy hh:mm aaa"); System.out.println(" Date " + f.format(date)); 

Output:

  Date 13 Jan 2010 01:01 AM 

The code seems fine, but still I get the wrong month name. Please help !! Thanks.

+4
source share
2 answers

Your template uses a minute instead of a month. It should be:

 yyyy-MM-dd HH:mm:ss 
+18
source

mm stands for minute. You should use mm when parsing the month:

 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
+7
source

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


All Articles