溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

android 獲取UTC時間和與.net時間戳的轉(zhuǎn)換

發(fā)布時間:2020-07-05 18:49:17 來源:網(wǎng)絡(luò) 閱讀:4712 作者:niceheart 欄目:移動開發(fā)

    本文純屬整合,將在項目中用到的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

先來個名詞解釋:
Epoch time:指從1970年1月1日零時起到現(xiàn)在為止的"second(秒) 數(shù)".
注意我給"second(秒) 數(shù)"加了引號,是因為在不一樣的項目中,計量單位可能是不同的,需要仔細(xì)的閱讀相關(guān)文檔.比如Gtalk Api的Gmail Notifications文檔中,所使用的date數(shù)為從1970年1月1日零時起到現(xiàn)在為止的"millisecond(毫秒) 數(shù)".
C#的Datetime.ticks:指從0001年1月1日零時起到現(xiàn)在為止的one ten-millionth of a second數(shù)量,或者one hundred nanoseconds of a second數(shù)量,也就是"千萬分之一秒"的數(shù)量.
java的Date.getTime():這個方法返回目標(biāo)時間到1970年1月1日零時為止的"millisecond(毫秒) 數(shù)".

然后來做個轉(zhuǎn)換:
1 second(秒)=1000 millisecond(毫秒)=10 x 100 0000 one ten-millionth of a second(千萬分之一秒)

好了,接下來是我們的java轉(zhuǎn)換函數(shù)

 public static long GetTicks(String epochStr)
 {
  //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)));
   String[] ds=date.split("/");
     
   //start of the ticks time
  Calendar calStart=Calendar.getInstance();
  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;
 }

用圖來說明:

    |       |         |
目標(biāo)時間  1970年    0001年

我是分別取得目標(biāo)時間和0001年到1970年的"millisecond(毫秒) 數(shù)",然后加在一起,這樣就得到了目標(biāo)時間到0001年的"millisecond(毫秒) 數(shù)",然后把這個數(shù)字換算成"千萬分之一秒"的數(shù)量,得到ticks數(shù).
或許你會發(fā)現(xiàn),為什么0001年的計算從1月3號起,不是應(yīng)該1月1號嗎.這個問題我也很奇怪,因為我發(fā)現(xiàn)如果從1月1號起,時間上就總是差著兩天,這原因等待高手來解決 :)

注意:.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)行分享,謝謝!歡迎探討!

向AI問一下細(xì)節(jié)

免責(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)容。

AI