gettime()返回值
java和javascript都支持时间类型date,他们的gettime()方法返回的是毫秒数。
默认返回的是13位数字,单位是毫秒。
注意事项
/**
* returns the number of milliseconds since january 1, 1970, 00:00:00 gmt
* represented by this <tt>date</tt> object.
*
* @return the number of milliseconds since january 1, 1970, 00:00:00 gmt
* represented by this date.
*/
public long gettime() {
return gettimeimpl();
}
容易造成误解的地方
如果程序运行在东八区,它返回北京时间1970年01月01日08时00分00秒起至现在东八区时间的总毫秒数。
如果运行在utc时区则返回1970年01月01日00时00分00秒起至当前utc时间的总毫秒数。
看起来似乎gettime()方法获取的时间戳与程序所运行的时区有关。
perdió的解释非常好,摘抄如下:
其实不是的,gettime()本身是没有问题,取到的timestamp就是从1970-01-01 00:00:00(utc)起到当前的毫秒数。
与程序真实运行的容器(服务器)所在的时区无关。
东八区"北京时间1970年01月01日08时00分00秒"不就是utc的1970年01月01日00时00分00秒吗。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论