当前位置: 移动技术网 > 移动技术>移动开发>Android > android将一个long型转成时间字符串

android将一个long型转成时间字符串

2018年09月12日  | 移动技术网移动技术  | 我要评论

服务器给传一个long型的,从1970年到一个时间的秒数,然后,转成一个时间字符串展示出来。。。

写了一个函数,如下:

[html]view plaincopy

publicstaticstringchargesecondstonowtime(stringseconds){

longtime=long.parselong(seconds)*1000-8*3600*1000;

simpledateformatformat2=newsimpledateformat("yyyy-mm-ddhh:mm");

returnformat2.format(newdate(time));

}

第一次,传进来的是秒数,直接调用函数后发现怎么都不对,时间差很多。查看函数定义,发现函数需要传递一个

毫秒单位,所以,需要将传进来的秒先乘1000转化成毫秒。然后调用后发现与现在时间差8小时,这个时候想起来时区的问题。

北京时间嘛,需要减去一个8小时的毫秒8*3600*1000.调用后返回的结果正常~~

同时,date类型转long型,如下:

[html]view plaincopy

//date要转换的date类型的时间

publicstaticlongdatetolong(datedate){

returndate.gettime();

}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网