I have the following line in my java code where I am trying to format a date string
ZonedDateTime zonedDateTime= ZonedDateTime.ofInstant(instant, tz);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm aa");
return zonedDateTime.format(formatter);
At line 2 , I am getting this error:
java.lang.IllegalArgumentException: Too many pattern letters: a
at java.time.format.DateTimeFormatterBuilder.parseField(DateTimeFormatterBuilder.java:1774)
How can I create a format string for DateTimeFormatter.ofPattern that will give e.g:
2023-04-21 7.00pm
Use one
aAs the error describes, you have used too many
acharacter codes.Change
aatoa.Localization
You may have been thinking of two letters for AM and PM. But actually, other text might appear instead.
The text generated by
adepends on the human language and cultural norms used during localization. You specify the language and norms by way of aLocale. If omitted, your JVM’s current default locale is implicitly applied.Example code
For fun, let’s try looking at all possible outputs across all locales.
See this code run live at IdeOne.com.
You can see that many different pieces of text are possible, many are more than two characters. Even those using
PMorpmmay have more than two characters when including FULL STOP character:p.m..