优质回答 回答者:雪学生
一、获取毫秒数的代码:
(1)System.currentTimeMillis() 这种方式速度最快。
(2)Calendar.getInstance().getTimeInMillis() 这种方式速度最慢。
二、获取微秒数的代码:
微秒使用System.nanoTime()方法:如果Java程序需要高精度的计时,如1毫秒或者更小,使用System.nanoTime()方法,可以满足需求。
扩展资料:
获取微秒函数System.nanoTime() 的隐患:
System.currentTimeMillis() 起始时间是基于 1970.1.1 0:00:00 这个确定的时间的,而System.nanoTime()是基于cpu核心的时钟周期来计时,它的开始时间是不确定的。
但是在多核处理器上,由于每个核心的开始时间不确定,那么
“long start = System.nanoTime();String ip = Utilities.getIpByUrl(url);long cost = System.nanoTime() - start;”
这段代码有可能会运行在两个不同的cpu核心上,从而导致得到的结果完全不符逻辑。
-----------------------------------------------------------------
回答者:忘了回家的路
public static void main(String[] args) {
// TODO Auto-generated method stub
Date dt= new Date();
Long time= dt.getTime();//这就是距离1970年1月1日0点0分0秒的毫秒数
System.out.println(System.currentTimeMillis());//与上面的相同
}
至于java中取微秒数,似乎不准确。精确不到微秒级别。有时还与操作系统有关。
-----------------------------------------------------------------
回答者:那一个胖子
public class TestTime {
public static void main(String[] args){
Date d = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");//其中yyyy-MM-dd是你要表示的格式
// 可以任意组合,不限个数和次序;具体表示为:MM-month,dd-day,yyyy-year;kk-hour,mm-minute,ss-second;
String str=sdf.format(d);
System.out.println("The date is : "+ str);
}
}
-----------------------------------------------------------------
回答者:刘志国
很简单的什么import都不用
long time = System.currentTimeMillis();
这个long的time就是你要的时间了 想怎么用随便你
TAGS: 毫秒单位