2015-04-28 81 views
1

在一个项目中使用乔达时间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); 

这不正是我想要的东西。它分析具有或不具有分数的日期,并且打印它们而没有分数。

如果我升级到最新版本的乔达时间,我得到

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[]),打印机实际上没有打印任何东西,但都没有工作。

+0

“你们两个都不工作”是什么意思?每种情况下会发生什么? –

+1

public DateTimeFormatterBuilder appendOptional(DateTimeParser parser)附加一个可选的解析器元素。如果没有匹配的打印机,则无法使用此DateTimeFormatterBuilder构建打印机。 ' http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormatterBuilder.html#appendOptional%28org.joda.time.format.DateTimeParser%29 这是一个错误修复在2.2 http://www.joda.org/joda-time/upgradeto220.html – NeplatnyUdaj

+0

@NeplatnyUdaj我知道 - 但这仍然是我想要的行为,现在还不清楚如何实现它,现在该bug已修复。 – juell

回答

2

所以我终于想通了这一点,解决的办法是单独构建完整的解析器和打印机,就像这样:

/** 
    * 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); 
0

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

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 toParser() 内部的方法来创建使用所有在所附元件DateTimeParser实例。 大多数应用程序不会使用此方法。如果您想在应用程序中使用解析器,请调用Formatter()并仅使用解析API。

对此构建器的后续更改不会影响返回的解析器。

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

http://www.joda.org/joda-time/apidocs/index.html

这可能是更加有用的实际

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

对此构建器的后续更改不会影响返回的格式化程序。

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

抛出: UnsupportedOperationException - 如果没有打印或解析支持

+0

appendOptional()仅使用解析器,但是 - 如何指定格式器为可选? – juell

+0

你可以发布使用打印功能的代码吗? –

+0

这几乎只是 FORMAT_LOCAL.print(新的DateTime(timeMillis.longValue(),区域); OP中的代码在格式化程序的静态实例化上失败,但不是在使用情况下 – juell