Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimeFormatter.parseLocalDate throws UnsupportedOperationException

Tags:

java

jodatime

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?

like image 615
FGreg Avatar asked May 02 '13 20:05

FGreg


People also ask

How to parse a date with an offset in datetimeformatter?

DateTimeFormatter With Predefined Instances DateTimeFormatter comes with multiple predefined date/time formats that follow ISO and RFC standards. For example, we can use the ISO_LOCAL_DATE instance to parse a date such as ‘2018-03-09': To parse a date with an offset, we can use ISO_OFFSET_DATE to get an output like ‘2018-03-09-03:00':

What is parselocaldatetime in Java?

Since: 2.0 parseLocalDateTime public LocalDateTimeparseLocalDateTime(String text) Parses only the local date-time from the given text, returning a new LocalDate. This will parse the text fully according to the formatter, using the UTC zone. Once parsed, only the local date-time will be used.

What is datetimeformatter with predefined instances?

DateTimeFormatter With Predefined Instances DateTimeFormatter comes with multiple predefined date/time formats that follow ISO and RFC standards. For example, we can use the ISO_LOCAL_DATE instance to parse a date such as ‘2018-03-09':

What is datetimeformatter ISO_local_date?

Field Detail ISO_LOCAL_DATE public static final DateTimeFormatter ISO_LOCAL_DATE The ISO date formatter that formats or parses a date without an offset, such as '2011-12-03'. This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended local date format.


1 Answers

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
like image 75
Ilya Avatar answered Oct 01 '22 22:10

Ilya