Java 使用可选解析器从joda time DateTimeFormatter打印

Java 使用可选解析器从joda time DateTimeFormatter打印,java,datetime,jodatime,Java,Datetime,Jodatime,在使用joda time 2.1的项目中,我有以下DateTimeFormatter: /** * Parser for the "fraction" part of a date-time value. */ private static final DateTimeParser FRACTION_PARSER = new DateTimeFormatterBuilder() .appendLiteral('.') .a

在使用joda time 2.1的项目中,我有以下
DateTimeFormatter

   /**
   * Parser for the "fraction" part of a date-time value.
   */
  private static final DateTimeParser FRACTION_PARSER =
      new DateTimeFormatterBuilder()
          .appendLiteral('.')
          .appendFractionOfSecond(3, 9)
          .toParser();

  /**
   * A formatter for a "local" date/time without time zone offset
   * (in the format "yyyy-dd-mmThh:mm:ss[.fff]").
   */
  private static final DateTimeFormatter FORMAT_LOCAL =
      new DateTimeFormatterBuilder()
          .append(ISODateTimeFormat.date())
          .appendLiteral('T')
          .append(ISODateTimeFormat.hourMinuteSecond())
          .appendOptional(FRACTION_PARSER)
          .toFormatter()
          .withZone(DateTimeZone.UTC);
这正是我想要的。它解析带分数或不带分数的日期,并打印不带分数的日期

如果我升级到joda time的最新版本,我会

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.UnsupportedOperationException: Printing is not supported
    at org.joda.time.format.DateTimeFormatterBuilder.toPrinter(DateTimeFormatterBuilder.java:136)

所以我猜我之前犯的是个错误,但它确实做了我想做的!我如何在不出错的情况下获得相同的行为?我试过使用
append(DateTimePrinter,DateTimeParser[])
和空打印机以及实际上不打印任何内容且两者都不起作用的打印机

您需要使用打印机的原因是什么。这对你有用吗?它为我编译并输出正确的日期/时间

private static final DateTimeParser FRACTION_PARSER =
        new DateTimeFormatterBuilder()
                .appendLiteral('[')
                .appendLiteral('.')
                .appendFractionOfSecond(3, 9)
                .appendLiteral(']')
                .toParser();

/**
 * A formatter for a "local" date/time without time zone offset
 * (in the format "yyyy-dd-mmThh:mm:ss[.fff]").
 */
private static final DateTimeFormatter FORMAT_LOCAL =
        new DateTimeFormatterBuilder()
                .append(ISODateTimeFormat.date())
                .appendLiteral('T')
                .append(ISODateTimeFormat.hourMinuteSecond())

                .appendOptional(FRACTION_PARSER)
                        .toFormatter()
                        .withZone(DateTimeZone.UTC);

String strInputDateTime = "1990-12-11T13:12:22[.025]";

DateTime dt = FORMAT_LOCAL.parseDateTime(strInputDateTime);

System.out.println(dt);
在阅读Javdoc时,提到了toParser();未使用,而是使用toFormatter();我希望这有帮助

托帕尔瑟

公共日期时间分析器toParser() 使用所有附加元素创建DateTimeParser实例的内部方法。 大多数应用程序不会使用此方法。如果希望在应用程序中使用解析器,请调用toFormatter()并仅使用解析API

对该生成器的后续更改不会影响返回的解析器

抛出: UnsupportedOperationException-如果不支持解析

实际上,这可能更有用

公共日期TimeFormatter到Formatter() 使用所有附加元素构造DateTimeFormatter。 这是应用程序在构建过程结束时用来创建可用格式化程序的主要方法

对该生成器的后续更改不会影响返回的格式化程序

返回的格式化程序可能不支持打印和解析。方法DateTimeFormatter.isPrinter()和DateTimeFormatter.isParser()将帮助您确定格式化程序的状态

抛出: UnsupportedOperationException-如果既不支持打印也不支持解析


因此,我最终发现,解决方案是分别构建完整的解析器和打印机,如下所示:

  /**
   * Parser for the "fraction" part of a date-time value.
   */
  private static final DateTimeParser FRACTION_PARSER =
      new DateTimeFormatterBuilder()
          .appendLiteral('.')
          .appendFractionOfSecond(3, 9)
          .toParser();

  private static final DateTimeParser BASE_PARSER =
      new DateTimeFormatterBuilder()
          .append(ISODateTimeFormat.date())
          .appendLiteral('T')
          .append(ISODateTimeFormat.hourMinuteSecond())
          .appendOptional(FRACTION_PARSER)
          .toParser();

  private static final DateTimePrinter BASE_PRINTER =
      new DateTimeFormatterBuilder()
          .append(ISODateTimeFormat.date())
          .appendLiteral('T')
          .append(ISODateTimeFormat.hourMinuteSecond())
          // omit fraction of second
          .toPrinter();

  /**
   * A formatter for a "local" date/time without time zone offset
   * (in the format "yyyy-dd-mmThh:mm:ss[.fff]").
   */
  private static final DateTimeFormatter FORMAT_LOCAL =
      new DateTimeFormatterBuilder()
          .append(BASE_PRINTER, BASE_PARSER)
          .toFormatter()
          .withZone(DateTimeZone.UTC);

你所说的“两者都不起作用”到底是什么意思?每种情况下会发生什么?
public DateTimeFormatterBuilder appendOptional(DateTimeParser parser)只附加一个可选的解析器元素。如果没有匹配的打印机,则无法从此DateTimeFormatterBuilder生成打印机这是一个在2.2@NeplatnyUdaj中修复的错误,我知道-但这仍然是我想要的行为,现在还不清楚如何实现它,因为这个错误已经修复。@JonSkeet无法创建没有打印任何内容(只是空字符串文字)的打印机(与上面相同的异常)。如果我将空打印机传递给append(DateTimePrinter,DateTimeParser[]),则生成的DateTimeFormatter不是打印机,稍后将拒绝打印。appendOptional()只接受解析器,但是-如何将格式化程序指定为可选?您能发布使用打印功能的代码吗?它几乎只是格式化本地.print(新的DateTime(timeMillis.longValue(),zone);不过,OP中的代码在格式化程序的静态实例化时失败,而不是在使用时失败。