java获取当前时间戳的方法
来源:互联网 发布:蓝月传奇虎威附灵数据 编辑:程序博客网 时间:2024/06/07 13:23
获取当前时间戳
//方法 一System.currentTimeMillis();//方法 二Calendar.getInstance().getTimeInMillis();//方法 三new Date().getTime();
获取当前时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式String date = df.format(new Date());// new Date()为获取当前系统时间,也可使用当前时间戳
获取时间戳三种方法执行效率比较:
import java.util.Calendar;import java.util.Date; public class TimeTest { private static long _TEN_THOUSAND=10000; public static void main(String[] args) { long times=1000*_TEN_THOUSAND; long t1=System.currentTimeMillis(); testSystem(times); long t2=System.currentTimeMillis(); System.out.println(t2-t1); testCalander(times); long t3=System.currentTimeMillis(); System.out.println(t3-t2); testDate(times); long t4=System.currentTimeMillis(); System.out.println(t4-t3); } public static void testSystem(long times){//use 188 for(int i=0;i<times;i++){ long currentTime=System.currentTimeMillis(); } } public static void testCalander(long times){//use 6299 for(int i=0;i<times;i++){ long currentTime=Calendar.getInstance().getTimeInMillis(); } } public static void testDate(long times){ for(int i=0;i<times;i++){ long currentTime=new Date().getTime(); } } }
执行结果:
133
2372
137
Calendar.getInstance().getTimeInMillis() 这种方式速度最慢,这是因为Canlendar要处理时区问题会耗费较多的时间。
阅读全文
0 0
- java获取当前时间戳的方法
- java获取当前时间戳的方法
- Java获取当前时间的年月日方法
- Java 获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- java获取当前日期时间的方法
- java获取当前时间的方法
- Java获取当前时间的年月日方法
- 2017企业信息安全峰会暨安全+沙龙年会邀请函
- 关于spark中dataframe数据处理
- Java集合四之List总结
- Python学习之pip安装
- Llinxu的命令提示符设置(ps)
- java获取当前时间戳的方法
- 介绍 Java 中的内存泄漏
- @SpringBootApplication注解
- request_threaded_irq及IRQF_ONESHOT的简单研究
- oracle 批量删除当前用户下以xxxx开头的表
- localhot本地储存刷新本地数据还在的问题
- springboot中出现无法跳转到前台JSP页面
- 【学术】Yang、Chen等人的科研经验
- 利用深度摄像头生成仿激光数据并构建地图