2013-03-01 85 views
1

這裏去我的代碼,但改變時區後,獲得相同的時間戳。更改時間戳到另一個GMT +時間戳

TimeZone timeZone = TimeZone.getTimeZone("GMT+11"); 
Calendar cal1 = Calendar.getInstance(); 
Calendar cal2 = Calendar.getInstance(); 

cal1.setTimeInMillis(Long.parseLong(classListDto.get(i) 
    .getClassStartTime()) * THOUSAND); 
cal2.setTimeInMillis(Long.parseLong(classListDto.get(i) 
    .getClassEndTime()) * THOUSAND); 
cal1.setTimeZone(timeZone); 
cal2.setTimeZone(timeZone); 

long startTimestamp = cal1.getTimeInMillis(); 
long endTimestamp = cal2.getTimeInMillis(); 

回答

1

嘗試,而不是在你的Calendar實例設置時區這樣的事情。

TimeZone timeZone = TimeZone.getTimeZone("GMT+11"); 
Calendar cal1 = Calendar.getInstance(); 
cal1.setTimeInMillis(Long.parseLong(classListDto.get(i).getClassStartTime()) * THOUSAND); 

Date tempDate = cal1.getTime(); 
SimpleDateFormat df = new SimpleDateFormat(); 
SimpleDateFormat df1 = new SimpleDateFormat(); 

df.setTimeZone(timeZone); 
Date gmtDate = df1.parse(df.format(tempDate)); // Put this in a Try/Catch block 

long startTimestamp = gmtDate.getTime(); 

希望這有助於!我知道它蹩腳到,雖然使用2個SimpleDateFormat對象。