calendar.getTimeInMillis() 和 System.currentTimeMillis() 的区别
@Test
public void test01(){
Calendar calendar=Calendar.getInstance();
// calendar.set(2019,06,04,16,42,50);
long date1=calendar.getTimeInMillis();
long date2=System.currentTimeMillis();
Date turnDate1=new Date(date1);
Date turnDate2=new Date(date2);
SimpleDateFormat fm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(fm.format(turnDate1));
System.out.println(fm.format(turnDate2)); }
两个时间是一样的。
2.性能比较
/**
* 各循环十万次获取时间戳
*/
@Test
public void test03(){
Calendar calendar=Calendar.getInstance();
long startTime=System.currentTimeMillis();
for(long i=0;i<100000;i++){
long date1=calendar.getTimeInMillis();
}
System.out.println("calender花费的时间:"+(System.currentTimeMillis()-startTime)); long startTime2=System.currentTimeMillis();
for(long i=0;i<100000;i++){
long date2=System.currentTimeMillis();
}
System.out.println("calender花费的时间:"+(System.currentTimeMillis()-startTime2));
}
本机测试发现值不稳定。查看源码System.currentTimeMillis 使用的nativae 方法,猜测是这种取时间更快。欢迎指正
calendar.getTimeInMillis() 和 System.currentTimeMillis() 的区别的更多相关文章
- System.nanoTime与System.currentTimeMillis的区别
平时产生随机数时我们经常拿时间做种子,比如用 System.currentTimeMillis的结果,但是在执行一些循环中使用了System.currentTimeMillis,那么每次的结 果将会差 ...
- System.nanoTime与System.currentTimeMillis的区别(转)
原文地址:http://blog.csdn.net/dliyuedong/article/details/8806868 平时产生随机数时我们经常拿时间做种子,比如用System.currentTim ...
- new Date().getTime()和System.currentTimeMillis()的区别
在Java中,new Date().getTime()和System.currentTimeMillis()都是用来获取当前时间的,并可以用DateFormat转成对应的时间格式,代码如下. impo ...
- 我的Java开发学习之旅------>System.nanoTime与System.currentTimeMillis的区别
首先来看一道题:下面代码的输出结果是什么? import java.util.HashMap; import java.util.Map; public class HashMapTest { pub ...
- Unix时间戳和Java 的 System.currentTimeMillis()的区别
- System.nanoTime与System.currentTimeMillis的理解与区别
System类代表系统,系统级的很多属性和控制方法都放置在该类的内部.该类位于java.lang包. 平时产生随机数时我们经常拿时间做种子,比如用System.currentTimeMillis的结果 ...
- System.currentTimeMillis() uptimeMillis elapsedRealtime 区别
System.currentTimeMillis() 系统时间,也就是日期时间,可以被系统设置修改,然后值就会发生跳变. uptimeMillis 自开机后,经过的时间,不包括深度睡眠的时间 ela ...
- 系统当前时间system.currenttimemillis与new Date().getTime() 区别
system.currenttimemillis //取到毫秒数,并且执行效率高 new Date().getTime()没他精确
- Java中使用new Date()和System.currentTimeMillis()获取当前时间戳的区别(转)(Java进阶-性能提升)
在开发过程中,通常很多人都习惯使用new Date()来获取当前时间,使用起来也比较方便,同时还可以获取与当前时间有关的各方面信息,例如获取小时,分钟等等,而且还可以格式化输出,包含的信息是比较丰富的 ...
随机推荐
- HDU1074(状态压缩DP)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU2874(LCA应用:求两点之间距离,图不连通)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- moco实例
一.moco模拟接口响应json moco的下载地址见虫师博客园:https://www.cnblogs.com/fnng/p/7511539.html foo.json文件内容如下 [ { &quo ...
- Flutter实战视频-移动电商-42.详细页_UI主页面架构搭建
42.详细页_UI主页面架构搭建 详细分成六大部分拆分开 body里面用FutureBuilder异步加载. FutureBuilder里面的furure属性这里用一个方法,必须返回的也是future ...
- HDU - 1495 非常可乐 bfs互倒三杯水
非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- ZOJ3164【区间dp】
题意: 有n个人,有一种关系叫做8g关系,给出m个关系,给出n个人的阵列 问你最多能拿走多少人,拿走以后相邻就是相邻了 思路: 典型的区间dp: dp[i][j] 代表 i-j 最多能去多少人: 如 ...
- laravel V层引入css 和js方法
引入css 默认引入public目录 <link rel="stylesheet" href="{{URL::asset('css/xxx.css')}}&quo ...
- jzoj5988. 【WC2019模拟2019.1.4】珂学计树题 (burnside引理)
传送门 题面 liu_runda曾经是个喜欢切数数题的OIer,往往看到数数题他就开始刚数数题.于是liu_runda出了一个数树题.听说OI圈子珂学盛行,他就在题目名字里加了珂学二字.一开始liu_ ...
- 第四章 “我要点爆”微信小程序云开发之疯狂点击与糖果点爆页面制作
疯狂点击点爆方式页面制作 疯狂点击为用户提供一个60秒的按钮点击时间,同时点击过程中有背景音乐,系统根据用户点击按钮的此时来进行热度值的计算. <view class="the_hea ...
- PJzhang:国内常用威胁情报搜索引擎说明
猫宁!!! 参考链接: https://www.freebuf.com/column/136763.html https://www.freebuf.com/sectool/163946.html 如 ...