如何将 Joda-Time 的 DateTimeFormat.forStyle() 转换为 JSR 310 JavaTime? [英] How to covert Joda-Time's DateTimeFormat.forStyle() to JSR 310 JavaTime?

查看:28
本文介绍了如何将 Joda-Time 的 DateTimeFormat.forStyle() 转换为 JSR 310 JavaTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我致力于将 Grails Joda-Time 插件转换为 JavaTime.

I working on convertion Grails Joda-Time plugin to JavaTime.

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

And I've old Joda time code like this:

    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) 接受样式.

How can I conver it to JSR 310? I can't find anything similar to method forStyle(String style) which accepts 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 规范重现:

But it fails of Instant type. Spock spec to reproduce:

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"
}

此测试失败并出现错误:

This test fails with error:

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?

So, why formatter can't format Instant?

推荐答案

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

The methods ofLocalizedDate(), ofLocalizedTime() and ofLocalizedDateTime() provide the localized formats.

要格式化 Instant,需要时区.这可以使用 withZone() 添加到格式化程序:

To format an Instant a time-zone is required. This can be added to the formatter using withZone():

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

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

Without the zone, JSR-310 formatter has no knowledge of how to convert the instant to human date-time fields.

这篇关于如何将 Joda-Time 的 DateTimeFormat.forStyle() 转换为 JSR 310 JavaTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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