s-day01-System类获取当前系统时刻
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package com.inmind.system06;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
/*
|
||||
System类获取当前系统时刻
|
||||
static long currentTimeMillis() 返回当前时间(以毫秒为单位)。
|
||||
|
||||
作用:以毫秒级别测试一段代码的执行效率,Instant以纳秒级别测试代码的执行效率
|
||||
*/
|
||||
public class SystemDemo15 {
|
||||
public static void main(String[] args) {
|
||||
// 获取当前时间
|
||||
Date date = new Date();
|
||||
long now2 = System.currentTimeMillis();
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
Instant now1 = Instant.now();
|
||||
|
||||
System.out.println(date);
|
||||
System.out.println(date.getTime());
|
||||
System.out.println(now1.getNano());
|
||||
System.out.println(now2);
|
||||
|
||||
System.out.println("------------以毫秒级别测试一段代码的执行效率------------");
|
||||
long start = System.currentTimeMillis();
|
||||
//测试500次打印消耗的时间(毫秒级)
|
||||
for (int i = 0; i < 500; i++) {
|
||||
System.out.println(i);
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println("打印消耗的毫秒值:"+(end-start));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user