Migration 如何隐藏乔达的时间';s DateTimeFormat.forStyle()到JSR 310 JavaTime?
我正在将Grails Joda Time插件转换为 我有一个古老的约达时间代码,像这样:Migration 如何隐藏乔达的时间';s DateTimeFormat.forStyle()到JSR 310 JavaTime?,migration,java-8,jodatime,java-time,Migration,Java 8,Jodatime,Java Time,我正在将Grails Joda Time插件转换为 我有一个古老的约达时间代码,像这样: def style switch (type) { case LocalTime: style = '-S' break case LocalDate: style = 'S-' break default: style =
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?
我找不到任何类似于接受风格的方法
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
?JSR 310有其他方法来替代样式:DateTimeFormatter.ofcalizedtime()
,DateTimeFormatter.ofcalizeddate()
和DateTimeFormatter.ofcalizeddatetime()
另一个问题是,Instant
类型无法格式化
JSR 310还有其他替代样式的方法:
DateTimeFormatter.ofCalizedTime()
,DateTimeFormatter.ofCalizedDate()
和DateTimeFormatter.ofCalizedDateTime()
另一个问题是,Instant
类型无法格式化
calizeddate()的
方法、calizedtime()的方法和calizeddatetime()的方法提供了本地化格式
要格式化即时
格式,需要时区。可以使用withZone()
将其添加到格式化程序中:
如果没有这个区域,JSR-310格式化程序就不知道如何将即时日期时间字段转换为人工日期时间字段 CalizedDate()
的方法、CalizedTime()的方法和CalizedDateTime()的方法提供了本地化格式
要格式化即时
格式,需要时区。可以使用withZone()
将其添加到格式化程序中:
如果没有这个区域,JSR-310格式化程序就不知道如何将即时日期时间字段转换为人工日期时间字段 我发现了一个奇怪的问题,可能是个bug我发现了一个奇怪的问题,可能是个bug
DateTimeFormatter formatter =
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale(UK)
.withZone(ZoneId.systemDefault());