[英]Print from joda-time DateTimeFormatter with optional Parser
在使用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[])
與空打印機和打印機一起使用,該打印機實際上不打印任何內容,而且它們都不起作用。
因此,我最終弄清楚了,解決方案是分別構造完整的解析器和打印機,如下所示:
/**
* 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);
是否有需要使用打印機的原因。 這對您有用嗎? 它為我編譯並輸出正確的日期/時間。
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
public DateTimeParser toParser()使用所有附加元素創建DateTimeParser實例的內部方法。 大多數應用程序將不會使用此方法。 如果要在應用程序中使用解析器,請調用toFormatter()並僅使用解析API。
此構建器的后續更改不會影響返回的解析器。
拋出:UnsupportedOperationException-如果不支持解析
http://www.joda.org/joda-time/apidocs/index.html
實際上這可能更有用
public DateTimeFormatter toFormatter()使用所有附加元素構造一個DateTimeFormatter。 這是應用程序在構建過程結束時用來創建可用格式化程序的主要方法。
對此構建器的后續更改不會影響返回的格式化程序。
返回的格式化程序可能不支持打印和解析。 DateTimeFormatter.isPrinter()和DateTimeFormatter.isParser()方法將幫助您確定格式化程序的狀態。
拋出:UnsupportedOperationException-如果不支持打印和解析
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.