您好,登錄后才能下訂單哦!
本文純屬整合,將在項目中用到的UTC時間和與.NET時間戳的轉(zhuǎn)換進(jìn)行記錄。
1、android獲取UTC時間
/** * 獲取UTC時間 * * @return */ public static String getUTCTimeStr() { DateFormat format = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss"); StringBuffer UTCTimeBuffer = new StringBuffer(); // 1、取得本地時間: Calendar cal = Calendar.getInstance(); // 2、取得時間偏移量: int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET); // 3、取得夏令時差: int dstOffset = cal.get(java.util.Calendar.DST_OFFSET); // 4、從本地時間里扣除這些差量,即可以取得UTC時間: cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset)); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int day = cal.get(Calendar.DAY_OF_MONTH); int hour = cal.get(Calendar.HOUR_OF_DAY); int minute = cal.get(Calendar.MINUTE); int second = cal.get(Calendar.SECOND); UTCTimeBuffer.append(year).append("/").append(month).append("/") .append(day); UTCTimeBuffer.append("/").append(hour).append("/").append(minute) .append("/").append(second); try { format.parse(UTCTimeBuffer.toString()); return UTCTimeBuffer.toString(); } catch (ParseException e) { e.printStackTrace(); } catch (java.text.ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } |
只需直接拷貝去使用即可。
2、獲取時間戳
/** * 獲取時間戳 * * @param dateCur * @return */ public static long GetTicks(String dateCur) { // convert the target-epoch time to a well-format string // String date = new java.text.SimpleDateFormat("yyyy/MM/dd/HH/mm/ss") // .format(new Date(Long.parseLong(epochStr))); // SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss"); // String dateCur = sdf.format(new Date()); String[] ds = dateCur.split("/"); // start of the ticks time Calendar calStart = Calendar.getInstance();
/** * 此處的參數(shù)很重要,原則上都是1,日所以為2,是因為之前的日期沒減掉1 第三個參數(shù)為1:日期多了2天,為2則日期多1天 * **/ //上傳失敗時這里總會出現(xiàn)混亂的情況,需要找到源頭解決 // calStart.set(1, 1, 0, 0, 0, 0); calStart.set(1, 1, 3, 0, 0, 0); // the target time Calendar calEnd = Calendar.getInstance(); calEnd.set(Integer.parseInt(ds[0]), Integer.parseInt(ds[1]), Integer.parseInt(ds[2]), Integer.parseInt(ds[3]), Integer.parseInt(ds[4]), Integer.parseInt(ds[5])); // epoch time of the ticks-start time long epochStart = calStart.getTime().getTime(); // epoch time of the target time long epochEnd = calEnd.getTime().getTime();
// get the sum of epoch time, from the target time to the ticks-start // time long all = epochEnd - epochStart; // convert epoch time to ticks time long ticks = ((all / 1000) * 1000000) * 10; return ticks; } |
將第一步獲取的UTC時間傳給第二步,即可獲取時間戳!
對于時間戳的解釋,我將引用一篇文章來說明,個人其實也是在探索中:
java的Date.getTime()轉(zhuǎn)換成C#的Datetime.ticks先來個名詞解釋: 然后來做個轉(zhuǎn)換: 好了,接下來是我們的java轉(zhuǎn)換函數(shù) public static long GetTicks(String epochStr) 用圖來說明: | | | 我是分別取得目標(biāo)時間和0001年到1970年的"millisecond(毫秒) 數(shù)",然后加在一起,這樣就得到了目標(biāo)時間到0001年的"millisecond(毫秒) 數(shù)",然后把這個數(shù)字換算成"千萬分之一秒"的數(shù)量,得到ticks數(shù). |
注意:.net里確實是從0001年01月01日開始。 不過歷史上因為歷法的轉(zhuǎn)換, 有“丟失的2天”。
個人在項目中發(fā)現(xiàn)一個問題,calStart.set(1, 1, 3, 0, 0, 0); 這里設(shè)置的時候會在不同的時間發(fā)生不同的變化,導(dǎo)致最后設(shè)置的時間也也發(fā)生變化,有的系統(tǒng)是從1970/01/01開始計算,有的會從1970/01/02開始,導(dǎo)致我拿到的時間都不一致,每次出問題就需要更改這里的設(shè)置(calStart.set(1, 1, 3, 0, 0, 0))就恢復(fù)正常了,如果有朋友也發(fā)生這樣的問題,請分享一下,本人將不甚感激,本人如果研究出來了也會更新進(jìn)行分享,謝謝!歡迎探討!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。