1. 程式人生 > >Android 時間戳處理 夏令時

Android 時間戳處理 夏令時

產品都出貨了,才發現國外有個夏令時,一開始想著還得根據時區來計算,那得寫多少判斷啊,還好找到了解決辦法,順便記錄一下吧~

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);