问题
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
?
回答1:
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
来源:https://stackoverflow.com/questions/16347261/datetimeformatter-parselocaldate-throws-unsupportedoperationexception