在我的android應用程序中,我必須解析格式爲EEE, dd MMM yyyy HH:mm:ss zzz
的日期。我正在使用下面的方法從中提取時間。這工作完美。但時間總是以GMT格式顯示,所以我必須將其轉換爲用戶時區,因爲在解析之前我已在代碼中添加了一行,代碼爲inputFormatter.setTimeZone(TimeZone.getDefault());
,因爲這不起作用我已更改參數在TimeZone.getTimeZone("UTC")
,TimeZone.getTimeZone("Asia/Kolkata")
等等,但沒有任何作品,時間仍然以格式。將GMT時區轉換爲用戶特定時區
這實際上是什麼問題?我怎麼能解決這個問題,任何幫助表示讚賞
public static String extractTime(String dateInput) {
Date date = null;
String time;
try {
DateFormat inputFormatter = new SimpleDateFormat(mFormat, Locale.getDefault());
date = inputFormatter.parse(dateInput);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat outputFormatter = new SimpleDateFormat("HH:mm:ss");
time = outputFormatter.format(date);
return time;
}
更新
我已經嘗試設置時區以outputFormatter而不是inputFormatter,但它仍然是相同的,在輸出沒有變化。
例子:
輸入:Tue, 21 Jul 2015 09:02:30 GMT
輸出獲得:05:02:30
您應該將timeZone設置爲'outputFormatter'而不是'inputFormatter'。 – Codebender
讓我試試 – droidev
@Codebender沒有改變它是一樣的 – droidev