3 回答

TA貢獻1856條經(jīng)驗 獲得超17個贊
你的格式化程序的模式是錯誤的。缺少年份 ( "yyyy"
) 且時區(qū)不匹配。要匹配它,您需要同時使用兩者z
,并且Z
還需要為 GMT 添加不匹配的文本,例如"'GMT'Z (z)"
.
試試這個:
"E MMM dd yyyy HH:mm:ss 'GMT'Z (z)"

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

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)));
添加回答
舉報