出于某种原因,我总是很难正确显示时间戳,但无论如何这是我的问题。
我正在从中提取事件,Calendar Provider API
并且某些事件(例如US Holidays
日历事件)采用 UTC,因此时间戳不是设备上应有的时间戳(当然,除非设备位于该时区)。
我有一个时间戳,1374105600000
它是07/18/2013 00:00:00 UTC
7 月 18 日午夜 UTC。我想要的是 7 月 18 日午夜本地设备时间的时间戳。
这就是我所做的
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
cal.setTimeInMillis(time);
long offset = tz.getRawOffset(); //gives me -18000000 5hr difference since I am in EST which I think is actually wrong because it should be a 4hr difference now with DST
因此,如果我将其添加到 UTC 时间戳
long local = time+offset;
它给了我不正确的时间july 17th at 3:00PM
如果我减去时间
long local = time-offset;
我仍然得到错误的时间,它给了我,july 18th at 1:00AM
但我认为我什至不应该减去,因为这对+
时区差异的人不起作用。
我在做什么错,为什么我不能获得正确的偏移量来获得正确的时间?
我也将其用作参考链接