android 獲取UTC時間和與.net時間戳的轉換

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

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

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

好了,接下來是我們的java轉換函數

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

用圖來說明:

    |       |         |
目標時間  1970年    0001年

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

注意:.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))就恢復正常了,如果有朋友也發生這樣的問題,請分享一下,本人將不甚感激,本人如果研究出來了也會更新進行分享,謝謝!歡迎探討!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章