在一个项目中使用乔达时间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[])
,打印机实际上没有打印任何东西,但都没有工作。
“你们两个都不工作”是什么意思?每种情况下会发生什么? –
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
@NeplatnyUdaj我知道 - 但这仍然是我想要的行为,现在还不清楚如何实现它,现在该bug已修复。 – juell