2011-05-22 91 views
29

我變換UTC時間到另一個時區,使用這種方法:轉換UTC日期,以其它時區

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
Date parsed = format.parse("2011-03-01 15:10:37"); 
TimeZone tz = TimeZone.getTimeZone("America/Chicago"); 
format.setTimeZone(tz); 

String result = format.format(parsed); 

所以輸入是2011-03-01 15:10:37但這(結果的值)的輸出爲2011-03-01 05:40:37。雖然它似乎關閉,並根據this link,它應該是2011-03-01 09:10:37

我在做什麼錯?

回答

62

原來的代碼幾乎是正確的,我並沒有考慮到的是,解析String時,最初得到Date對象,它使用默認的系統TimeZone,所以源日期是不是在UTC因爲我預期。

訣竅是將日期解析爲UTC時設置時區,然後將其設置爲目標TimeZone。例如:

SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC")); 
Date parsed = sourceFormat.parse("2011-03-01 15:10:37"); // => Date is in UTC now 

TimeZone tz = TimeZone.getTimeZone("America/Chicago"); 
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
destFormat.setTimeZone(tz); 

String result = destFormat.format(parsed); 
+2

不要忘了'嘗試{}趕上(ParseException的E){}''左右sourceFormat.parse()'。此外,爲了獲得手機所在的本地時區,請使用'TimeZone.getDefault()',以便將UTC轉換爲當地時區 – 2013-02-13 00:32:49

+0

@hadi Eskandari Mate您剛剛救了我!我正在試圖解決我的頭髮問題......!我從我們的服務器上拉下數據並存儲到數據庫中,然後再顯示到屏幕上。服務器日期存儲在AEST(澳大利亞東部標準)中,並且我在使用默認系統時區時遇到了確切的問題,因此解釋錯誤的時間。當我嘗試更改爲另一個日期時,它總是錯誤的:)我只需要'formater.setTimeZone(TimeZone.getTimeZone(RBApplication.adjustTimezoneAEST));'在我的解析行之前。歡呼,我在這個問題上幾個小時! – wired00 2013-02-26 07:43:38

+0

@ wired00很高興聽到這有助於隊友:) – 2013-02-26 10:16:24

2

您需要考慮夏令時。我以毫秒爲單位計算偏移量(從UTC開始)。像這樣的東西應該工作。

int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(parsed) ? tz.getDSTSavings() : 0); 
String result = format.format(parsed.getTime() + currentOffsetFromUTC); 

inDayLightTime(...)方法返回一個布爾並且爲了,以決定如果「日期」代表期間DST週期或一個不必須通過一個日期的對象。

11

將日期格式「2011-06-23T15:11:32」的字符串轉換爲出時區。

private String getDate(String dateString) { 
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
    formatter.setTimeZone(TimeZone.getTimeZone("UTC")); 
    Date value = null; 
    try { 
     value = formatter.parse(dateString); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
    SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy hh:mmaa"); 
    dateFormatter.setTimeZone(TimeZone.getDefault()); 
    String dt = dateFormatter.format(value); 

    return dt; 
} 
1

您可以根據您的要求解析您的日期格式和時區。 試試這段代碼,我希望它對你有幫助。

private String getFormattedDate(String OurDate) { 
    try { 
     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // According to your Server TimeStamp 
     formatter.setTimeZone(TimeZone.getTimeZone("UTC")); //your Server Time Zone 
     Date value = formatter.parse(OurDate); // Parse your date 

     SimpleDateFormat dateFormatter = new SimpleDateFormat("MM-dd-yyyy"); //this format changeable according to your choice 
     dateFormatter.setTimeZone(TimeZone.getDefault()); 
     OurDate = dateFormatter.format(value); 

    } catch (Exception e) { 
     OurDate = "00-00-0000 00:00"; 

    } 
    return OurDate; 
} 
0

以下代碼對我來說可以很好地將日期從一個tz更改爲另一個tz。它也考慮DayLightSaving。

public static Calendar changeTimezoneOfDate(Date date, TimeZone fromTZ, TimeZone toTZ) { 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(date); 
    long millis = calendar.getTimeInMillis(); 
    long fromOffset = fromTZ.getOffset(millis); 
    long toOffset = toTZ.getOffset(millis); 
    long convertedTime = millis - (fromOffset - toOffset); 
    Calendar c = Calendar.getInstance(); 
    c.setTimeInMillis(convertedTime); 
    return c; 
}