DateTimeFormatter.parseLocalDate throws UnsupportedOperationException
Asked Answered
B

2

7

The API for parseLocalDate says it will throw UnsupportedOperationException "if parsing is not supported". What does it mean by 'if parsing is not supported'? I'm looking through the source and can not find anywhere that throws UnsupportedOperationException. Has anyone ever been in a scenario where this exception was thrown from calling parseLocalDate?

Beverly answered 2/5, 2013 at 20:36 Comment(0)
O
7

DateTimeFormatter have two usages:

  • print dates;
  • parse dates;

When you create DateTimeFormatter instance, you pass to it DateTimePrinter and DateTimeParser.

If your formatter has only printer, and you want parse date - UnsupportedOperationException will be thrown.

If your formatter has only parser, and you want print date - UnsupportedOperationException will be thrown.

Example

  DateTimeFormatter formatter = new DateTimeFormatter(new DateTimePrinter()
  {
     // implements all abstract methods         
  }, null); // this instance has printer and hasn't parser
  formatter.print(new DateTime()); // works well
  formatter.parseDateTime("datetimestring"); // throws exeption
Osteogenesis answered 3/5, 2013 at 8:22 Comment(2)
Ah good to know, thank you. It looks as though org.joda.time.format.DateTimeFormat.forPattern(String) will always return a DateTimeFormatter with both a printer and a parser; so a DateTimeFormatter retrieved via forPattern should not ever throw UnsupportedOperationException correct?Beverly
Moreover, note that there are two similar instance methods: ISODateTimeFormat.dateTime() and ISODateTimeFormat.dateTimeParser(). If you used the latter and try for .format(dateTime), you get this exception.Downall
C
0

Wanted to asnwer this just because I want to call out @membersound comment to @Ilya answer as it was what was wrong with my code. Basically my team inherited some legacy code and who ever wrote the serializer for Joda DateTime set the formatter using ISODateTimeFormat.dateTimeParser() instead of ISODateTimeFormat.dateTime().

It especially confused me because the only error I saw at first was not an UnsupportedOperationException, but was instead logged as com.fasterxml.jackson.databind.JsonMappingException: Printing not supported, which I had a hard time finding when Googling it (as it was likely a recapturing of the actual error). I hope that helps someone else who spent maybe a little too long trying to figure out what was wrong like I did.

Cologarithm answered 22/4, 2022 at 15:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.