从joda-time DateTimeFormatter打印可选的Parser [英] Print from joda-time DateTimeFormatter with optional Parser

查看:258
本文介绍了从joda-time DateTimeFormatter打印可选的Parser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a project using joda-time 2.1, I have the following 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);

它正是我想要的。它会分析带有或不带分数的日期,并打印出没有分数的日期。

It does exactly what I want. It parses dates with or without fractions, and prints them without fractions.

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

If I upgrade to a more recent version of joda-time, I get

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 [])

So I guess what I had before was an error, but it did exactly what I wanted to do! How do I get the same behavior, without making an error? I've tried using append(DateTimePrinter, DateTimeParser[]) with a null printer and with a printer that doesn't actually print anything and neither of them work.

推荐答案

所以我最终想到了这个,解决方案是分别构建完整的解析器和打印机,如下所示:

So I eventually figured this out, the solution is to construct the complete parser and printer separately, like so:

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

这篇关于从joda-time DateTimeFormatter打印可选的Parser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆