Ось мій код, але отримання тієї ж часової позначки після зміни часового поясу.
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 для відповіді № 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
об'єкти, хоча.
0 для відповіді № 2
Ви можете побачити зміну часового поясу у своєму коді, якщо ви зробите це cal1.getField(Calendar.HOUR);
замість того, щоб просто читати мілісекунди. Календар, здається, зберігає мілі лише в термінах UTC: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#getTimeInMillis()