如何將“2021-11-20 01:00”格式的日期決議為ZonedDateTime?我正在嘗試:
String value = "2021-11-20 01:00";
ZonedDateTime zDate = ZonedDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-ddxxx"));
但是得到這個奇怪的錯誤:
java.time.format.DateTimeParseException:無法決議文本“2021-11-20 01:00”:無法從 TemporalAccessor 獲取 ZonedDateTime:{OffsetSeconds=3600},ISO 決議為 java 型別的 2021-11-20。時間.格式.決議
...不支持的欄位:InstantSeconds ...
有什么建議?歐洲 VIES VAT ID 檢查系統在接收 XML (SOAP) 回應時使用此時間格式:<requestDate>2021-11-20 01:00</requestDate>. 與OffsetDateTime..相同的錯誤
有趣的是,Javadoc 說“三個字母 (x) 輸出小時和分鐘,帶一個冒號,例如' 01:30'”。那么為什么上面的模式不起作用呢?
也試過這個 - 同樣的錯誤:
ZonedDateTime zDate = ZonedDateTime.parse(value, DateTimeFormatter.ISO_OFFSET_DATE);
完整的錯誤日志:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2021-11-20 01:00' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {OffsetSeconds=3600},ISO resolved to 2021-11-20 of type java.time.format.Parsed
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2017)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952)
at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:402)
at javaapplication5.JavaApplication5.checkVATatVIES(JavaApplication5.java:162)
at javaapplication5.JavaApplication5.main(JavaApplication5.java:65)
Caused by: java.time.DateTimeException: Unable to obtain OffsetDateTime from TemporalAccessor: {OffsetSeconds=3600},ISO resolved to 2021-11-20 of type java.time.format.Parsed
at java.base/java.time.OffsetDateTime.from(OffsetDateTime.java:370)
at java.base/java.time.format.Parsed.query(Parsed.java:235)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
... 3 more
Caused by: java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {OffsetSeconds=3600},ISO resolved to 2021-11-20 of type java.time.format.Parsed
at java.base/java.time.Instant.from(Instant.java:378)
at java.base/java.time.OffsetDateTime.from(OffsetDateTime.java:365)
... 5 more
Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds
at java.base/java.time.format.Parsed.getLong(Parsed.java:203)
at java.base/java.time.Instant.from(Instant.java:373)
... 6 more
使用 OpenJDK 11。
uj5u.com熱心網友回復:
沒有只包含日期和時區的 Java 型別。ZonedDateTime 和 OffsetDateTime 正如其名稱所暗示的那樣,包含日期和時間。因此,您需要創建一個格式化程式來假定一天中的默認時間:
String value = "2021-11-20 01:00";
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
builder.appendPattern("yyyy-MM-ddxxx");
builder.parseDefaulting(ChronoField.HOUR_OF_DAY, 0);
DateTimeFormatter formatter = builder.toFormatter();
ZonedDateTime zDate = ZonedDateTime.parse(value, formatter);
當然,一天中的小時可以是您選擇的任何值: 12例如,代表中午。但它必須是有效的。
uj5u.com熱心網友回復:
tl;博士
org.threeten.extra.OffsetDate.parse( "2021-11-20 01:00" )
org.threeten.extra.OffsetDate
Java 中內置的java.time類不提供將日期與偏移量相結合的概念的類。所以你的決議嘗試毫無意義。
該ThreeTen-EXTRA 專案的目的是補充的功能java.time。它包括OffsetDate滿足您特定需求的課程。將庫添加到您的專案中。
String input = "2021-11-20 01:00" ;
OffsetDate od = OffsetDate.parse( input ) ;
從該OffsetDate物件中,您可以獲得LocalDate零件或ZoneOffset零件。并且您可以確定其他日期時間值,例如添加一天中的時間以獲得OffsetDateTime.
你評論說:
如果我在 21 日上午 00:30 提出請求,我會得到包含 20 日日期的回應......因為我們的時間是 UTC 2,他們的時間是 UTC 1
順便說一句,提示:我建議養成使用完全形成的小時偏移量和填充零的習慣,也可以使用分鐘。雖然各種標準都允許,但我看到多個庫在縮寫時失敗。所以我推薦“ 02:00”而不是“UTC 2”。
以下是一些示例代碼,演示了您所描述的場景。我們使用時區是Africa/Brazzaville因為它在那一刻的偏移量為 01:00,我們使用Europe/Kaliningrad 02:00 作為其偏移量。
LocalDate ld = LocalDate.of( 2021 , Month.NOVEMBER , 21 );
LocalTime lt = LocalTime.of( 0 , 30 );
ZoneId zKaliningrad = ZoneId.of( "Europe/Kaliningrad" );
ZonedDateTime zdtKaliningrad = ZonedDateTime.of( ld , lt , zKaliningrad );
ZoneId zBrazzaville = ZoneId.of( "Africa/Brazzaville" );
ZonedDateTime zdtBrazzaville = zdtKaliningrad.withZoneSameInstant( zBrazzaville );
OffsetDate od = OffsetDate.from( zdtBrazzaville );
OffsetDateTime odt = od.atTime( LocalTime.MIN );
ZonedDateTime asSeenInKaliningrad = odt.atZoneSameInstant( zKaliningrad );
跑的時候。
zdtKaliningrad = 2021-11-21T00:30 02:00[Europe/Kaliningrad]
zdtBrazzaville = 2021-11-20T23:30 01:00[Africa/Brazzaville]
od = 2021-11-20 01:00
odt = 2021-11-20T00:00 01:00
asSeenInKaliningrad = 2021-11-20T01:00 02:00[Europe/Kaliningrad]
就個人而言,我質疑日期偏移概念的實際價值,但這與您的問題無關。
在特定時區作業通常比僅使用偏移量問題少。
我認為將日期與區域作為時刻跟蹤更有意義,時間軸上一天開始的時間點,以及命名的時區而不是偏移量。
String result =
LocalDate
.of( 2021 , 11 , 20 )
.atStartOfDay( ZoneId.of( "Africa/Brazzaville" ) )
.toString()
;
在 IdeOne.com 上查看此代碼的實時運行情況。
2021-11-20T00:00 01:00[非洲/布拉柴維爾]
對于表示一天的文本的資料交換,我建議2021-11-20T00:00 01:00[Africa/Brazzaville]以擴展ISO 8601的格式傳輸此字串,方法是在方括號中附加時區名稱。
uj5u.com熱心網友回復:
您可以像這樣將自己的模式指定為字串:
String str = "2021-11-20 01:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime date= LocalDateTime.parse(str, formatter);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/369603.html
上一篇:使用python決議json檔案
