【问题标题】:How to covert Joda-Time's DateTimeFormat.forStyle() to JSR 310 JavaTime?如何将 Joda-Time DateTimeFormatter.forStyle() 转换为 JSR 310 Java 时间?
【发布时间】:2015-02-11 04:41:30
【问题描述】:

我正在将 Grails Joda-Time 插件转换为 JavaTime

我有这样的旧 Joda 时间代码:

    def style
    switch (type) {
        case LocalTime:
            style = '-S'
            break
        case LocalDate:
            style = 'S-'
            break
        default:
            style = 'SS'
    }
    Locale locale = LocaleContextHolder.locale
    return DateTimeFormatter.ofPattern(style, locale).withResolverStyle(ResolverStyle.LENIENT)

如何将其转换为 JSR 310? 我找不到任何类似于接受样式的方法forStyle(String style) 的东西。

UPD 我找到了解决方法:

        Locale locale = LocaleContextHolder.locale
        DateTimeFormatter formatter
        switch (type) {
            case LocalTime:
                formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(locale)
                break
            case LocalDate:
                formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(locale)
                break
            default:
                formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(locale)
        }
        return formatter

但是 Instant 类型的失败。 Spock 规范重现:

def 'Instant locale formatting'() {
    given:
    Instant inst = Instant.ofEpochMilli(92554380000L)
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(UK)
    expect:
    formatter.format(inst) == "07/12/72 05:33"
}

此测试失败并出现错误:

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfMonth
    at java.time.Instant.getLong(Instant.java:603)
    at java.time.format.DateTimePrintContext$1.getLong(DateTimePrintContext.java:205)
    at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
    at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2543)
    at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182)
    at java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser.format(DateTimeFormatterBuilder.java:4350)
    at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182)
    at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1744)
    at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1718)

那么,为什么格式化程序不能格式化Instant

【问题讨论】:

    标签: migration java-8 jodatime java-time


    【解决方案1】:

    ofLocalizedDate()ofLocalizedTime()ofLocalizedDateTime() 方法提供本地化格式。

    要格式化Instant,需要一个时区。可以使用 withZone() 将其添加到格式化程序中:

    DateTimeFormatter formatter =
        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
                         .withLocale(UK)
                         .withZone(ZoneId.systemDefault());
    

    没有区域,JSR-310 格式化程序不知道如何将即时转换为人工日期时间字段。

    【讨论】:

    【解决方案2】:

    JSR 310 有其他方式替换样式:DateTimeFormatter.ofLocalizedTime()DateTimeFormatter.ofLocalizedDate()DateTimeFormatter.ofLocalizedDateTime()

    另外一个问题是Instant类型不能被格式化 Format Instant to String

    【讨论】:

      猜你喜欢
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 2018-01-09
      • 2013-10-22
      • 2017-11-13
      • 1970-01-01
      相关资源
      最近更新 更多