第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

ISO_DATE_TIME.format() 到具有可選偏移量的 LocalDateTime

ISO_DATE_TIME.format() 到具有可選偏移量的 LocalDateTime

智慧大石 2023-06-14 11:14:08
我正在嘗試將 ISO 日期時(shí)間轉(zhuǎn)換為 LocalDateTime:String timezone = "Pacific/Apia";String isoDateTime = "2011-12-03T10:15:30+03:00";var zoned = ZonedDateTime.from(ISO_DATE_TIME_FORMATTER.parse(isoDateTime));return zoned.withZoneSameInstant(ZoneId.of(timeZone)).toLocalDateTime();此代碼有效 - 它將其轉(zhuǎn)換為包含偏移量的本地日期。但問(wèn)題是當(dāng)我在沒(méi)有偏移的情況下傳遞日期時(shí):2011-12-03T10:15:30 -java.time.DateTimeException:無(wú)法從 TemporalAccessor 獲取 ZonedDateTime:{},ISO 解析為 java.time.format.Parsed 類型的 2011-12-03T10:15:30我知道為什么會(huì)出現(xiàn)此異常,問(wèn)題是如何將包括偏移量在內(nèi)的兩個(gè)日期都轉(zhuǎn)換為 LocalDateTime?. 我想避免一些字符串解析(檢查字符串是否包含“+”/“-”)。
查看完整描述

2 回答

?
慕森王

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個(gè)贊

您可以構(gòu)建一個(gè)帶有可選偏移量元素的解析器,并使用 TemporalAccessor.isSupported 檢查偏移量是否存在。


    DateTimeFormatter parser = new DateTimeFormatterBuilder()

        .parseCaseInsensitive()

        .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)

        .optionalStart()

        .appendOffsetId()

        .optionalEnd()

        .toFormatter();


    TemporalAccessor accessor = parser.parse(isoDateTime);

    if (accessor.isSupported(ChronoField.OFFSET_SECONDS)) {

        var zoned = ZonedDateTime.from(accessor);

        return zoned.withZoneSameInstant(ZoneId.of(timezone)).toLocalDateTime();

    }

    return LocalDateTime.from(accessor);


查看完整回答
反對(duì) 回復(fù) 2023-06-14
?
白衣染霜花

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊

您可以在子句中處理解析異常catch并嘗試不同的解析器。例如像這樣:


String timezone = "Pacific/Apia"

String isoDateTime = "2011-12-03T10:15:30+03:00";    

try{

    var zoned = ZonedDateTime.from(ISO_DATE_TIME_FORMATTER.parse(isoDateTime));

    return zoned.withZoneSameInstant(ZoneId.of(timeZone)).toLocalDateTime();

} catch (DateTimeException e) {

    //no time zone information -> parse as LocalDate

    return LocalDateTime.parse(isoDateTime);

}


查看完整回答
反對(duì) 回復(fù) 2023-06-14
  • 2 回答
  • 0 關(guān)注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)