2013-03-01 81 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对象。