i have a SimpleDateFormat format = new SimpleDateFormat("d M y H:m"); and i try to parse the String "8 Jan 2019 16:47" with it, but i get a ParseException. Did i create it the wrong way?
According to docs.oracle.com the M should recognize 3-letter-months.
Can anyone help me?
java: ParseException: Unparseable date
357 Views Asked by Ginso At
2
There are 2 best solutions below
0
On
The official documentation: (https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)
You probably missed out this little note here:
Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
Based on your example input, the following works:
SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy HH:mm");
java.time
The output from this snippet is:
What went wrong in your code?
SimpleDateFormatandDateare poorly designed and long outdated, the former in particular notoriously troublesome. I recommend you don’t use them in 2019.As others have said you need three
Mfor month abbreviation (no matter if you are using the outdatedSimpleDateFormator the modernDateTimeFormatter). OneMwill match a month number in 1 or 2 digits, for example1for January.You should also specify a locale for your formatter. I took
Janto be English so specifiedLocale.ENGLISH. If you don’t specify locale, the JVM’s default locale will be used, which may work well on some JVMs and suddenly break some day when the default locale has been changed or you are trying to run your program on a different computer.Link
Oracle tutorial: Date Time explaining how to use
java.time.