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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java GMT 日期時間解析

java GMT 日期時間解析

眼眸繁星 2023-01-05 15:33:22
我有一個像下面這樣的字符串Fri May 31 2019 05:08:40 GMT-0700 (PDT)我想將其轉(zhuǎn)換為類似yyyy-MM-dd.我試過了。String date1 = "Fri May 31 2019 05:08:40 GMT-0700 (PDT)";DateTimeFormatter f = DateTimeFormatter.ofPattern( "E MMM dd HH:mm:ss z uuuu" ).withLocale( Locale.US );ZonedDateTime zdt = ZonedDateTime.parse( date1 , f );LocalDate ld = zdt.toLocalDate();DateTimeFormatter fLocalDate = DateTimeFormatter.ofPattern( "yyyy-MM-dd" );String output = ld.format( fLocalDate ) ;我收到錯誤:Exception in thread "main" java.time.format.DateTimeParseException: Text 'Fri May 31 2019 05:08:40 GMT-0700 (PDT)' could not be parsed at index 13at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)at java.time.format.DateTimeFormatter.parse(Unknown Source)at java.time.ZonedDateTime.parse(Unknown Source)
查看完整描述

3 回答

?
慕慕森

TA貢獻1856條經(jīng)驗 獲得超17個贊

你的格式化程序的模式是錯誤的。缺少年份 ( "yyyy") 且時區(qū)不匹配。要匹配它,您需要同時使用兩者z,并且Z還需要為 GMT 添加不匹配的文本,例如"'GMT'Z (z)".

試試這個:

"E MMM dd yyyy HH:mm:ss 'GMT'Z (z)"


查看完整回答
反對 回復(fù) 2023-01-05
?
手掌心

TA貢獻1942條經(jīng)驗 獲得超3個贊

正如您在這里看到的,您可以使用以下模式:

public static void main(String[] args) throws Exception {

    String date1 = "Fri May 31 2019 05:08:40 GMT-0700 (PDT)";

    DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('z')'" ).withLocale( Locale.US );


    ZonedDateTime zdt = ZonedDateTime.parse( date1 , f );

    LocalDate ld = zdt.toLocalDate();

    DateTimeFormatter fLocalDate = DateTimeFormatter.ofPattern( "yyyy-MM-dd" );

    String output = ld.format( fLocalDate ) ;

    System.out.println(output);

}

輸出:


2019-05-31


查看完整回答
反對 回復(fù) 2023-01-05
?
慕絲7291255

TA貢獻1859條經(jīng)驗 獲得超6個贊

由于您只需要yyyy-MM-dd格式的日期,請嘗試以下代碼:


   String date1 = "Fri May 31 2019 05:08:40 GMT-0700";


   //this format is good enough to read required data from your String

   DateFormat df1 = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss");


   //format you require as final output

   DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");


   //convert String to date ( with required attributes ) and then format to target

   System.out.println(df2.format(df1.parse(date1)));


查看完整回答
反對 回復(fù) 2023-01-05
  • 3 回答
  • 0 關(guān)注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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