产品都出货了,才发现国外有个夏令时,一开始想着还得根据时区来计算,那得写多少判断啊,还好找到了解决办法,顺便记录一下吧~
Date bdate = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date smdate = sdf.parse("2000-1-1 00:00:00");
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
int baseDstOffest= cal.get(Calendar.DST_OFFSET);//2000年的偏移量
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
int curDstOffest= cal.get(Calendar.DST_OFFSET);//当前时间的偏移量
long between_days = (time2 - time1 - baseDstOffest + curDstOffest) / (1000);//946656000 为网页上面2000年一月一日的时间戳
Log.e("tag","时间间隔"+between_days+"--now=="+time2+"---2--"+time1);
版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.youkuaiyun.com/android_txf/article/details/81480012
本文介绍了一种处理夏令时影响的方法,通过计算不同日期之间的夏令时偏移量来准确获取时间间隔。该方法避免了复杂的时区判断逻辑,提供了一种简单有效的解决方案。
2358

被折叠的 条评论
为什么被折叠?



