java获取当前时间戳的方法
来源:互联网 发布:网络盒子看电视直播 编辑:程序博客网 时间:2024/06/07 20:45
文章转自:http://www.cnblogs.com/zhujiabin/p/6168671.html
获取当前时间戳
//方法 一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(); } }}
执行结果:
1332372137
Calendar.getInstance().getTimeInMillis() 这种方式速度最慢,这是因为Canlendar要处理时区问题会耗费较多的时间。
阅读全文
0 0
- java获取当前时间戳的方法
- java获取当前时间戳的方法
- Java获取当前时间的年月日方法
- Java 获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- Java获取当前时间的年月日方法
- java获取当前日期时间的方法
- java获取当前时间的方法
- Java获取当前时间的年月日方法
- C#编程入门21_网络编程
- [APP开发技巧] APICloud开发小技巧分享--持续更新,建议收藏
- 两种在Mac上删除tuxera ntfs的方法
- SpringMVC: Controller报java.lang.NoSuchMethod异常解决办法
- FFMPEG开源音视频项目学习汇总
- java获取当前时间戳的方法
- SpringCloud(第 034 篇)配置服务端ConfigServer设置安全认证
- : error: undefined reference to `cv::VideoCapture::open(std::__cxx11::basic_string<char, std::char_t
- 企业出现突发网络攻击,如何及时做应急响应?
- webpack 入门一篇足够,简单而又详细入门
- Swagger配置:Maven+SpringBoot+jdk1.
- 【bzoj 1029/CS 2913】建筑抢修 07JS(贪心+优先队列)
- 10.11
- The proper way of implementing user login system(mark learn)