2014-01-25 140 views
66

我真的很困惑我用Calendar.getInstance(TimeZone.getTimeZone("UTC"))方法調用得到的結果,它返回了IST時間。Calendar.getInstance(TimeZone.getTimeZone(「UTC」))沒有返回UTC時間

下面是我用

Calendar cal_Two = Calendar.getInstance(TimeZone.getTimeZone("UTC")); 
System.out.println(cal_Two.getTime()); 

的代碼,我得到的迴應是:

Sat Jan 25 15:44:18 IST 2014 

所以我試圖改變的默認時區爲UTC,然後我檢查了,那麼它工作正常

Calendar cal_Two = Calendar.getInstance(TimeZone.getTimeZone("UTC")); 
System.out.println(cal_Two.getTime()); 

TimeZone tz = TimeZone.getDefault() ; 
TimeZone.setDefault(TimeZone.getTimeZone("UTC")); 
Calendar cal_Three = Calendar.getInstance(); 
System.out.println(cal_Three.getTime()); 
TimeZone.setDefault(tz); 

結果:

Sat Jan 25 16:09:11 IST 2014 
Sat Jan 25 10:39:11 UTC 2014 

我在這裏錯過了什麼嗎?

+0

去掉了常規標籤,所有這一切都是純Java –

回答

101

System.out.println(cal_Two.getTime())調用從getTime()返回Date。它是Date它正在轉換爲一個字符串爲println,並且該轉換將使用您的情況下的默認IST時區。

您需要明確使用DateFormat.setTimeZone()才能在所需的時區打印Date

編輯:@Laurynas的禮貌,考慮一下:

TimeZone timeZone = TimeZone.getTimeZone("UTC"); 
Calendar calendar = Calendar.getInstance(timeZone); 
SimpleDateFormat simpleDateFormat = 
     new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US); 
simpleDateFormat.setTimeZone(timeZone); 

System.out.println("Time zone: " + timeZone.getID()); 
System.out.println("default time zone: " + TimeZone.getDefault().getID()); 
System.out.println(); 

System.out.println("UTC:  " + simpleDateFormat.format(calendar.getTime())); 
System.out.println("Default: " + calendar.getTime()); 
+0

謝謝,我知道了。 – vicky

+0

我一直在尋找這個片刻。也許標題不夠明確? 順便說一句,感謝您的幫助 –

+1

如果你顯示System.out.println :) –

-3

嘗試使用GMT而不是UTC。他們指的是同一時區,但名稱GMT更常見,可能會起作用。

+1

沒有真正相關 – ToolmakerSteve

+0

使用GMT將產生相同的結果。 – Jim

8

java.util.Date獨立於時區。當您打印cal_Two雖然Calendar的實例有它的時區設置爲UTCcal_Two.getTime()將返回其不具有時區一個Date實例(並始終處於默認的時區)

Calendar cal_Two = Calendar.getInstance(TimeZone.getTimeZone("UTC")); 
System.out.println(cal_Two.getTime()); 
System.out.println(cal_Two.getTimeZone()); 

輸出:

Sat Jan 25 16:40:28 IST 2014 
    sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null] 

TimeZone.setDefault()

的Javadoc

設置由getDefault方法返回的TimeZone。如果區域 爲空,請將默認值重置爲初次啓動VM 時的值。

因此,移動你的setDefault()之前cal_Two被實例化,你會得到正確的結果。

TimeZone.setDefault(TimeZone.getTimeZone("UTC")); 
Calendar cal_Two = Calendar.getInstance(TimeZone.getTimeZone("UTC")); 
System.out.println(cal_Two.getTime()); 

Calendar cal_Three = Calendar.getInstance(); 
System.out.println(cal_Three.getTime()); 

輸出:

Sat Jan 25 11:15:29 UTC 2014 
Sat Jan 25 11:15:29 UTC 2014 
+0

謝謝,我明白了。 – vicky

2
Calendar currentTime = Calendar.getInstance(TimeZone.getTimeZone("UTC")); 
currentTime.set(Calendar.ZONE_OFFSET, TimeZone.getTimeZone("UTC").getRawOffset()); 
Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR_OF_DAY, currentTime.get(Calendar.HOUR_OF_DAY)); 
calendar.getTimeInMillis() 

爲我工作

0
Following code is the simple example to change the timezone 
public static void main(String[] args) { 
      //get time zone 
      TimeZone timeZone1 = TimeZone.getTimeZone("Asia/Colombo"); 
      Calendar calendar = new GregorianCalendar(); 
      //setting required timeZone 
      calendar.setTimeZone(timeZone1); 
      System.out.println("Time :" + calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND)); 

     } 

,如果你想看到的時區的列表,這裏是follwing代碼

public static void main(String[] args) { 

    String[] ids = TimeZone.getAvailableIDs(); 
    for (String id : ids) { 
     System.out.println(displayTimeZone(TimeZone.getTimeZone(id))); 
    } 

    System.out.println("\nTotal TimeZone ID " + ids.length); 

} 

private static String displayTimeZone(TimeZone tz) { 

    long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset()); 
    long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset()) 
           - TimeUnit.HOURS.toMinutes(hours); 
    // avoid -4:-30 issue 
    minutes = Math.abs(minutes); 

    String result = ""; 
    if (hours > 0) { 
     result = String.format("(GMT+%d:%02d) %s", hours, minutes, tz.getID()); 
    } else { 
     result = String.format("(GMT%d:%02d) %s", hours, minutes, tz.getID()); 
    } 

    return result; 

}