本文純屬整合,將在項目中用到的UTC時間和與.NET時間戳的轉換進行記錄。
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();
/** * 此處的參數很重要,原則上都是1,日所以爲2,是因爲之前的日期沒減掉1 第三個參數爲1:日期多了2天,爲2則日期多1天 * **/ //上傳失敗時這裏總會出現混亂的情況,需要找到源頭解決 // 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()轉換成C#的Datetime.ticks先來個名詞解釋: 然後來做個轉換: 好了,接下來是我們的java轉換函數 public static long GetTicks(String epochStr) 用圖來說明: | | | 我是分別取得目標時間和0001年到1970年的"millisecond(毫秒) 數",然後加在一起,這樣就得到了目標時間到0001年的"millisecond(毫秒) 數",然後把這個數字換算成"千萬分之一秒"的數量,得到ticks數. |
注意:.net裏確實是從0001年01月01日開始。 不過歷史上因爲曆法的轉換, 有“丟失的2天”。
個人在項目中發現一個問題,calStart.set(1, 1, 3, 0, 0, 0); 這裏設置的時候會在不同的時間發生不同的變化,導致最後設置的時間也也發生變化,有的系統是從1970/01/01開始計算,有的會從1970/01/02開始,導致我拿到的時間都不一致,每次出問題就需要更改這裏的設置(calStart.set(1, 1, 3, 0, 0, 0))就恢復正常了,如果有朋友也發生這樣的問題,請分享一下,本人將不甚感激,本人如果研究出來了也會更新進行分享,謝謝!歡迎探討!