将时间戳更改为另一个gmt +时间戳

时间:2013-03-01 06:54:12

标签: android

这是我的代码,但在更改时区后得到相同的时间戳。

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();

2 个答案:

答案 0 :(得分: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个对象很糟糕。

答案 1 :(得分:0)

如果您执行cal1.getField(Calendar.HOUR);而不是仅读取毫秒,则可以在代码中看到时区更改。日历似乎仅以UTC格式存储millis:http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#getTimeInMillis()