出于某种原因,我总是最难获得时间戳来正确显示,但无论如何,这是我的问题。将UTC时间戳转换为本地设备时间戳
我从Calendar Provider API
拉事件和一些事件,如US Holidays
日历事件是UTC,所以时间戳不是它应该在设备上(除非该设备是在该时区当然)。
我有这个1374105600000
的时间戳,这是07/18/2013 00:00:00 UTC
所以七月十八号在午夜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
,但我不认为我应该减去,因为这将不会为+
时区差异的人工作。
我在做什么错,为什么我不能得到正确的偏移量以获得正确的时间?
我也是用这个像作为参考太Link