如何将Joda-Time的DateTimeFormat.forStyle()转换为JSR 310 JavaTime? [英] How to covert Joda-Time's DateTimeFormat.forStyle() to 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
我发现解决方法:
UPD I found workaround:
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
但是它没有 In stant
类型。 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屋!