【发布时间】:2016-02-11 05:04:11
【问题描述】:
我的时间戳基于 IST(印度标准时间/格林威治标准时间 + 5:30 小时),我的应用程序根据设备时间戳显示上次活动时间。 这是当前时间 - 活动时间戳。当设备时区是 IST 时很好。
例如,
Activity timestamp - 11/Feb/2016 09:00:00 AM
Current timestamp (IST) - 11/Feb/2016 10:00:00 AM
My Calculation = Current timestamp - Activity timestamp
So Application shows 1 hr ago
但设备时区同时更改为 PHT 等其他时区(菲律宾时间/格林威治标准时间 + 8 小时)
Activity timestamp - 11/Feb/2016 09:00:00 AM
Current timestamp(PHT) - 11/Feb/2016 12:30:00 AM (plus 2:30Hrs compare with IST)
My Calculation = Current timestamp - Activity timestamp
So Application shows 3 hrs 30 mis ago
我的问题是,如何始终使用 java 获取 IST 时间?无论时区如何,我都需要 IST 时间。
我尝试了下面的代码,当我将值更改为 IST 但时区自动更改为设备时区。
源码请参考以下网址http://goo.gl/dnvQF5
SimpleDateFormat sd = new SimpleDateFormat(
"yyyy.MM.dd G 'at' HH:mm:ss z");
Date date = new Date();
// TODO: Avoid using the abbreviations when fetching time zones.
// Use the full Olson zone ID instead.
sd.setTimeZone(TimeZone.getTimeZone("GMT"));
String gmtDate = sd.format(date);
System.out.println("GMT --> " + gmtDate);
String istDate = gmtDate.replace("GMT", "IST");
System.out.println("After Replace -> " + istDate);
sd.setTimeZone(TimeZone.getTimeZone("IST"));
try {
Date istConvertedDate = sd.parse(gmtDate);
System.out.println("After Convert --> " + istConvertedDate);
} catch (ParseException ex) {
ex.printStackTrace();
}
我得到了像
这样的输出GMT --> 2016.02.11 AD at 05:20:07 GMT
After Replace -> 2016.02.11 AD at 05:20:07 IST
After Convert --> Thu Feb 11 00:20:07 EST 2016
请帮我解决这个问题。
【问题讨论】:
-
使用名为 JODA TIME 的库
标签: java android datetime timezone