2017-06-22 82 views
2

我正在使用Guardian API獲取有關足球的最新新聞。從API響應中以字符串格式格式化日期和時間

我想向用戶顯示日期和時間信息,但不是以API將它引回給我的格式。

當查詢http://content.guardianapis.com/search?page-size=10&section=football&show-tags=contributor&api-key=test後請求webPublicationDate我得到這種格式的響應:

2017-06-22T16:18:04Z

現在,我想在這個日期和時間信息格式: 例如Jun 21,2017 and 16:18 or 4:18 pm

雖然我基本上知道正確格式化Date對象爲這種格式:

/** 
* Return the formatted date string (i.e. "Mar 3, 1984") from a Date object. 
*/ 
private String formatDate(Date dateObject) { 
    SimpleDateFormat dateFormat = new SimpleDateFormat("LLL dd, yyyy"); 
    return dateFormat.format(dateObject); 
} 

/** 
* Return the formatted date string (i.e. "4:30 PM") from a Date object. 
*/ 
private String formatTime(Date dateObject) { 
    SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a"); 
    return timeFormat.format(dateObject); 
} 

但我似乎無法讓我進入Date對象的響應轉換。

+1

而且重複的:https://開頭計算器。 com/q/2201925和https://stackoverflow.com/q/31090946 –

回答

0

您可以格式化文字是這樣的:

package com.mkyong.date; 

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

public class TestDateExample5 { 

public static void main(String[] argv) { 

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); 
    String dateInString = "2014-10-05T15:23:01Z"; 

    try { 

     Date date = formatter.parse(dateInString.replaceAll("Z$", "+0000")); 
     System.out.println(date); 

     System.out.println("time zone : " + TimeZone.getDefault().getID()); 
     System.out.println(formatter.format(date)); 

    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

} 

} 

ž後綴是指UTC,java.util.SimpleDateFormat不能正確分析它,你需要用「+0000」來取代標z 。從這裏

代碼:https://www.mkyong.com/java/how-to-convert-string-to-date-java/

0

,而不是與SimpleDateFormat直接合作(在這種老的API有lots of problemsdesign issues),你可以使用ThreeTen Backport,對Java 8的新的日期/時間類有很大反向移植。要在Android中使用它,您還需要ThreeTenABP(有關如何使用它的更多信息,請參閱here)。

要使用的主要類是org.threeten.bp.ZonedDateTime(可以解析日期/時間輸入)和org.threeten.bp.format.DateTimeFormatter(用於控制輸出格式)。

如果你正在閱讀這個領域(2017-06-22T16:18:04Z)爲String,你可以創建一個ZonedDateTime這樣的:

ZonedDateTime z = ZonedDateTime.parse("2017-06-22T16:18:04Z"); 

如果你已經有了一個java.util.Date對象,你可以使用org.threeten.bp.DateTimeUtilsorg.threeten.bp.ZoneOffset轉換它:

Date date = // get java.util.Date 
ZonedDateTime z = DateTimeUtils.toInstant(date).atZone(ZoneOffset.UTC); 

在結束時,ZonedDateTime對象將具有webPublicationDate值。

要獲得不同的輸出格式,只需爲每種格式創建一個DateTimeFormatter即可。在下面的例子中,我也用java.util.Locale類,以確保月份名稱都是英文:

// for Mar 3, 1984 
DateTimeFormatter f1 = DateTimeFormatter.ofPattern("MMM d, yyyy", Locale.ENGLISH); 
// for 4:40 PM 
DateTimeFormatter f2 = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH); 
// for 16:18 
DateTimeFormatter f3 = DateTimeFormatter.ofPattern("HH:mm", Locale.ENGLISH); 

System.out.println(f1.format(z)); // Jun 22, 2017 
System.out.println(f2.format(z)); // 4:18 PM 
System.out.println(f3.format(z)); // 16:18 

輸出是:

2017年6月22日
下午4時18
16 :18

請注意,它使用UTC時區(Z2017-06-22T16:18:04Z)。如果你想顯示在另一個時區的日期和時間,只需使用org.threeten.bp.ZoneId類:

System.out.println(f3.format(z.withZoneSameInstant(ZoneId.of("Europe/London")))); // 17:18 

輸出是17:18(becase的倫敦夏季時間了)。

請注意,API使用IANA timezones names(始終格式爲Continent/City,如America/Sao_PauloEurope/Berlin)。 避免使用3字母縮寫(如CST或),因爲它們是ambiguous and not standard。要找到更適合每個地區的時區,請使用ZoneId.getAvailableZoneIds()方法,並檢查哪一個最適合您的用例。


如果你不希望其他依賴添加到您的項目並使用SimpleDateFormat,你做同樣的事情(創建一個解析器和3輸出格式化,並使用英語語言環境)。另外不要忘記設置時區 - 我在下面使用UTC,但您可以將其更改爲您想要的任何時區。

// parse date 
String dateInString = "2017-06-22T16:18:04Z"; 
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); 
Date date = parser.parse(dateInString); 

// create output formatters (set timezone to UTC) 
TimeZone utc = TimeZone.getTimeZone("UTC"); 
SimpleDateFormat s1 = new SimpleDateFormat("MMM d, yyyy", Locale.ENGLISH); 
s1.setTimeZone(utc); 
SimpleDateFormat s2 = new SimpleDateFormat("h:mm a", Locale.ENGLISH); 
s2.setTimeZone(utc); 
SimpleDateFormat s3 = new SimpleDateFormat("HH:mm", Locale.ENGLISH); 
s3.setTimeZone(utc); 

System.out.println(s1.format(date)); 
System.out.println(s2.format(date)); 
System.out.println(s3.format(date)); 

的輸出將是相同的:

2017年6月22日
下午4點18
16:18