@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() 的区别的更多相关文章

  1. System.nanoTime与System.currentTimeMillis的区别

    平时产生随机数时我们经常拿时间做种子,比如用 System.currentTimeMillis的结果,但是在执行一些循环中使用了System.currentTimeMillis,那么每次的结 果将会差 ...

  2. System.nanoTime与System.currentTimeMillis的区别(转)

    原文地址:http://blog.csdn.net/dliyuedong/article/details/8806868 平时产生随机数时我们经常拿时间做种子,比如用System.currentTim ...

  3. new Date().getTime()和System.currentTimeMillis()的区别

    在Java中,new Date().getTime()和System.currentTimeMillis()都是用来获取当前时间的,并可以用DateFormat转成对应的时间格式,代码如下. impo ...

  4. 我的Java开发学习之旅------>System.nanoTime与System.currentTimeMillis的区别

    首先来看一道题:下面代码的输出结果是什么? import java.util.HashMap; import java.util.Map; public class HashMapTest { pub ...

  5. Unix时间戳和Java 的 System.currentTimeMillis()的区别

  6. System.nanoTime与System.currentTimeMillis的理解与区别

    System类代表系统,系统级的很多属性和控制方法都放置在该类的内部.该类位于java.lang包. 平时产生随机数时我们经常拿时间做种子,比如用System.currentTimeMillis的结果 ...

  7. System.currentTimeMillis() uptimeMillis elapsedRealtime 区别

    System.currentTimeMillis()  系统时间,也就是日期时间,可以被系统设置修改,然后值就会发生跳变. uptimeMillis 自开机后,经过的时间,不包括深度睡眠的时间 ela ...

  8. 系统当前时间system.currenttimemillis与new Date().getTime() 区别

    system.currenttimemillis //取到毫秒数,并且执行效率高 new Date().getTime()没他精确

  9. Java中使用new Date()和System.currentTimeMillis()获取当前时间戳的区别(转)(Java进阶-性能提升)

    在开发过程中,通常很多人都习惯使用new Date()来获取当前时间,使用起来也比较方便,同时还可以获取与当前时间有关的各方面信息,例如获取小时,分钟等等,而且还可以格式化输出,包含的信息是比较丰富的 ...

随机推荐

  1. tabbar 设置样式

    app.json配置文件中,使用时需要把注释删除,配置文件不支持备注 tabbar:{ color:'#fff',//字体颜色  需要时HexColor,设置成red 无法识别,下方颜色设置同理 se ...

  2. CS231n 2016 通关 第三章-SVM 作业分析

    作业内容,完成作业便可熟悉如下内容: cell 1  设置绘图默认参数 # Run some setup code for this notebook. import random import nu ...

  3. sizeToFit的学习与认知

    今天一扫前两日的坏心情,终于有心情平静下来,今天我是根据网络上的一些资料进行学习,今天学习的内容是 sizeToFit() 方法在不方便手动布局的场景中的使用. 首先感谢资料的提供者:参考1 参考2 ...

  4. HDOJ-1021

    Fibonacci Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. python使用xlrd操作Excel文件

    一.xlrd读取Excel文件 用xlrd进行读取比较方便,流程和平常手动操作Excel一样,打开工作簿(Workbook),选择工作表(sheets),然后操作单元格(cell). 例子:要打开当前 ...

  6. Websocket 学习

    一.含义 WebSocket 是一种在单个TCP连接上进行全双工通讯的协议.   WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在WebSocket ...

  7. linux网络编程中的超时设置

    1 下面是在网上找到的资料,先非常的感谢. 用setsockopt()来控制recv()与send()的超时 在send(),recv()过程中有时由于网络状况等原因,收发不能预期进行,而设置收发超时 ...

  8. C#字典常用技巧

    说明    必须包含名空间System.Collection.Generic     Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)     键必须是唯一的,而值不需要唯 ...

  9. Flutter实战视频-移动电商-38.路由_Fluro中Handler编写方法

    38.路由_Fluro中Handler编写方法 在main.dart中初始化Fluro 编写handler 在lib下新建routers文件夹,表示里面要很多路由相关的文件 我们声明一个Handler ...

  10. python-os.walk()使用举例

    文件目录结构 dir 1 1 1.txt 2.txt 3.txt 2 2.txt 3 4 4.txt 3.txt 1.txt 2 2.txt 3 3.txt dir.txt 代码: import os ...