代码点读机 / Java / System.currentTimeMillis

System.currentTimeMillis Java · 系统

返回当前时间与 1970 年 1 月 1 日 00:00:00 UTC 之间相差的毫秒数,常用来计时。

怎么用

long t = System.currentTimeMillis();

小例子

long t0 = System.currentTimeMillis();
// 执行一些代码
long 耗时 = System.currentTimeMillis() - t0;
System.out.println("耗时:" + 耗时 + "ms");

运行结果

输出耗时毫秒数,例如 15ms

提醒:如果需要更高精度或日期计算,考虑使用 java.time.Instant

官方文档:https://docs.oracle.com/javase/8/docs/api/java/lang/System.html

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Java全部词条