java测试方法运行时间 System.currentTimeMillis();

前端之家收集整理的这篇文章主要介绍了java测试方法运行时间 System.currentTimeMillis();前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

测试:

@H_301_6@


效果




System.currentTimeMillis();


currentTimeMillis()返回以毫秒为单位确当前时间,返回的是当前时间与调和世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位丈量)。注意,当返回值的时间单位是毫秒时,值的粒度取决于基础操作系统,并且粒度可能更大。例如,许多操作系统以几10毫秒为单位丈量时间。


主要用法


(1)用来测试程序的运行时间:
  如图中所示



(2)控制线程时间,刷新屏幕频率:

time1 = System.currentTimeMillis(); ------程代码段------ time2 = System.currentTimeMillis(); if (time2 - time1 < 60) { try { Thread.sleep(60 - (time2 - time1)); } catch (InterruptedException e) { } }


(3)生成不重复的文件名:
public String getName(){ Stringdate1 = null; SimpleDateFormatsdf1 = new SimpleDateFormat("yyyyMMddHHmmssSSS"); date1= sdf1.format(new Date(System.currentTimeMillis()))+".txt"; return date1; }

(4)获得日期类型

long currentTime = System.currentTimeMillis(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒") Date date = new Date(currentTime); System.out.println(formatter.format(date)); 运行结果以下: 当前时间:2015年-01月16日⑴6时42分46秒


猜你在找的PHP相关文章