欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

    本文純屬整合,將在項(xiàng)目中用到的UTC時(shí)間和與.NET時(shí)間戳的轉(zhuǎn)換進(jìn)行記錄。

成都創(chuàng)新互聯(lián)自2013年起,先為邛崍等服務(wù)建站,邛崍等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為邛崍企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

    1、android獲取UTC時(shí)間

/**

* 獲取UTC時(shí)間

* @return

*/

public static String getUTCTimeStr() {

DateFormat format = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss");

StringBuffer UTCTimeBuffer = new StringBuffer();

// 1、取得本地時(shí)間:

Calendar cal = Calendar.getInstance();

// 2、取得時(shí)間偏移量:

int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);

// 3、取得夏令時(shí)差:

int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);

// 4、從本地時(shí)間里扣除這些差量,即可以取得UTC時(shí)間:

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、獲取時(shí)間戳

/**

* 獲取時(shí)間戳

* @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,是因?yàn)橹暗娜掌跊](méi)減掉1 第三個(gè)參數(shù)為1:日期多了2天,為2則日期多1天

* **/

//上傳失敗時(shí)這里總會(huì)出現(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時(shí)間傳給第二步,即可獲取時(shí)間戳!

    對(duì)于時(shí)間戳的解釋,我將引用一篇文章來(lái)說(shuō)明,個(gè)人其實(shí)也是在探索中:

java的Date.getTime()轉(zhuǎn)換成C#的Datetime.ticks

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

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

好了,接下來(lái)是我們的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;
 }

用圖來(lái)說(shuō)明:

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

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

注意:.net里確實(shí)是從0001年01月01日開(kāi)始。 不過(guò)歷史上因?yàn)闅v法的轉(zhuǎn)換, 有“丟失的2天”。

   個(gè)人在項(xiàng)目中發(fā)現(xiàn)一個(gè)問(wèn)題,calStart.set(1, 1, 3, 0, 0, 0);  這里設(shè)置的時(shí)候會(huì)在不同的時(shí)間發(fā)生不同的變化,導(dǎo)致最后設(shè)置的時(shí)間也也發(fā)生變化,有的系統(tǒng)是從1970/01/01開(kāi)始計(jì)算,有的會(huì)從1970/01/02開(kāi)始,導(dǎo)致我拿到的時(shí)間都不一致,每次出問(wèn)題就需要更改這里的設(shè)置(calStart.set(1, 1, 3, 0, 0, 0))就恢復(fù)正常了,如果有朋友也發(fā)生這樣的問(wèn)題,請(qǐng)分享一下,本人將不甚感激,本人如果研究出來(lái)了也會(huì)更新進(jìn)行分享,謝謝!歡迎探討!

網(wǎng)頁(yè)題目:android獲取UTC時(shí)間和與.net時(shí)間戳的轉(zhuǎn)換
文章URL:http://chinadenli.net/article0/ihopoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)外貿(mào)建站商城網(wǎng)站網(wǎng)站制作搜索引擎優(yōu)化全網(wǎng)營(yíng)銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名