[英]Android get only time from iso-8601
登錄用戶時,我正在對服務器進行API調用。 API調用返回具有以下字段的JSON格式數據:
time_logged_in-ISO-8601格式的時間,例如2013-08-19T14:29Z
但是在我的TextView中,我只想顯示用戶登錄的時間,例如14:29。
誰能幫我告訴我該怎么做?
使用SimpleDateFormat從一種格式轉換為另一種格式。 然后,您可以從中補充相關部分。 一種方法如下:
String server_format = "2013-08-19T14:29Z"; //server comes format ?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
try {
Date date = sdf.parse(server_format);
System.out.println(date);
String your_format = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
String [] splitted = your_format.split(" ");
System.out.println(splitted[1]); //The second part of the splitted string, i.e time
// Now you can set the TextView here
} catch (ParseException e) {
System.out.println(e.toString()); //date format error
}
PS我還沒有測試此代碼。 希望您能理解。 請參閱如何格式化日期android? 更多。
另一種方法是使用.split()
使用T
作為分隔符)。 然后,僅使用從字符串中刪除所有出現的char給出的答案刪除Z
部分,並在TextView
顯示結果。
第三種方法是使用.replace
函數將T
和Z
替換為" "
(空格)。 然后使用.split()
您可以使用" "
作為分隔符來分割字符串。 返回的數組的第二個元素將給您時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.