2015-04-24 51 views
2

我從服務器獲取數據是這樣的:的Android使用的SimpleDateFormat

data = "2015-04-24T23:00:17+08:00" 

我在UTC + 8,我想獲得

data = "2015-04-24 23:00:17" 

我設定此

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); 
Data = formatter.parse(data); 

但我得到那

Fri Apr 24 23:00:17 格林尼治標準時間+0800 2015 

我該如何糾正它?

如果我使用

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-M-dd HH:mm:ss",Locale.ENGLISH); 

我得到

Fri Apr 24 23:00:17 GMT+08:00 2015 

這不是我想要

+1

嘗試'的SimpleDateFormat格式=新的SimpleDateFormat( 「YYYY-MM-DD HH:MM:SS」,Locale.ENGLISH);' – Tarun

回答

0

一旦你解析你從服務器返回的日期,你有一個java.util.Date對象。如果你想轉換成你自己的格式,你必須再次格式化日期對象。

private String getDate(String serverDate) { 
    SimpleDateFormat serverFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); 
    try { 
     Date date = serverFormatter.parse(serverDate); 

     SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     String formattedDate = myFormatter.format(date); 

     return formattedDate; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return ""; 
} 
+0

非常感謝你! – ivae

0

您所需的輸出,你需要的日期格式更改爲

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);