2013-10-17 56 views
1

我會設置日曆實例的時區,但我無法做到。 我已經嘗試:日曆:正確設置時區

DateFormat dateFormat = new SimpleDateFormat("dd//MM/yyyy HH:mm:ss"); 
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/Rosario")); 
System.out.println(cal.getTime()); 

(我用「美洲/羅薩里奧」隨機),但我總是得到我的當前時間。什麼是正確的模式來做到這一點?

回答

2

你應該DateFormat對象設置時區:

DateFormat df = new SimpleDateFormat("dd//MM/yyyy HH:mm:ss", Locale.getDefault()); 
df.setTimeZone(TimeZone.getTimeZone("America/Rosario")); 

// Will print the formatted date-time in the America/Rosario timezone 
System.out.println(df.format(new Date())); 
+0

PS:僅供參考我沒有檢查是否'「美洲/羅薩里奧」'是一個有效的時區 – anubhava

+0

我不需要使用日期()對象,但日曆。 – giozh

+0

這也可以使用'Calendar cal = Calendar.getInstance();' – anubhava