温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android 获取UTC时间和与.net时间戳的转换

发布时间:2020-07-05 18:49:17 来源:网络 阅读:4702 作者:niceheart 栏目:移动开发

    本文纯属整合,将在项目中用到的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))就恢复正常了,如果有朋友也发生这样的问题,请分享一下,本人将不甚感激,本人如果研究出来了也会更新进行分享,谢谢!欢迎探讨!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI