Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

DateTimeFormatter – java.lang.IllegalArgumentException: Too many pattern letters: a

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

2023-04-21 7.00pm

>Solution :

Use one a

As the error describes, you have used to many a character codes.

👉 Change aa to a.

You may have been thinking of two letters for AM and PM. But actually, other text might appear instead. The text generated by a depends on the human language and cultural norms used during localization. You specify the language and norms by way of a Locale. If omitted, your JVM’s current default locale is implicitly applied.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading