tl; dr
이 작업은 실패합니다.FormatStyle을 LONG 또는 FULL로 사용하여 지역화 된 포맷터에서 OffsetDateTime이 실패합니다.
OffsetDateTime.now()
.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
) // throws DateTimeException.
동일한 오프셋을 사용하여 동일한 시간에 ZonedDateTime
이 작동합니다.
왜?
세부
포맷터가 SHORT
또는 MEDIUM
의 FormatStyle
을 수행하는 경우 format
작품을 호출, java.time
자동 DateTimeFormatter.ofLocalizedDateTime
를 통해 OffsetDateTime
의 문자열 표현을 현지화시키는. 그러나 포맷터가 LONG
또는 FULL
인 경우 DateTimeException
이 발생합니다. 그러나 ZonedDateTime
은 동일한 offset과 동일한 순간을 사용하여 성공합니다. 왜?
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG) ;
OffsetDateTime odt = OffsetDateTime.now(ZoneId.systemDefault()) ;
ZonedDateTime zdt = odt.atZoneSameInstant(odt.getOffset()) ; // Generate a `ZonedDateTime` with same moment and same offset as the `OffsetDateTime`.
// Succeeds.
String outputZdt = zdt.format(f) ;
System.out.println("outputZdt: " + outputZdt) ;
// Fails. Throws exception.
if (false) {
String outputOdt = odt.format(f) ; // Throws exception.
System.out.println("outputOdt: " + outputOdt) ;
}
이 부분은 code run live at IdeOne.com입니다.
실행시 ...
좋습니다.
outputZdt : 2017년 9월 16일 오전 8시 42분 14초의 Z
나쁜.
Exception in thread "main" java.time.DateTimeException: Unable to extract value: class java.time.OffsetDateTime
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:282)
at java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.format(DateTimeFormatterBuilder.java:3682)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
at java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser.format(DateTimeFormatterBuilder.java:4347)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
at java.time.OffsetDateTime.format(OffsetDateTime.java:1674)
at Ideone.main(Main.java:28)
나는 발생을 제외하고,
odt.atZoneSameInstant(odt.getOffset())
를 해결하기 위해 해당 코드의 핵심을 썼다. 그런 다음 깨달았습니다. 왜
java.time
내부적으로 같은 일을하지 않습니까? 왜
OffsetDateTime
은 같은 순간과 동일한 오프셋을 가진
ZonedDateTime
이 성공할 때 포맷하지 못합니까? 이 전환을
OffsetDateTime
에서
ZonedDateTime
으로 변경해야하는 이유는 무엇입니까?
➟이 동작이 버그 또는 기능에 실패 했습니까?
나는 버그 보고서를 제출 하겠지만, 내가 뭔가를 오해하고 있는지 확인하고 싶다.
ZoneId zone = context.getValue(TemporalQueries.zoneId());
그것은 추출을 시도 : 코드를 디버깅
는