DateTimeFormatter.parseLocalDate throws UnsupportedOperationException

淺唱寂寞╮ 提交于 2019-12-08 19:38:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!