使用Instant对象来完成
关键代码:
获取当前时间
Instant now = Instant.now();
获取当前时间距离1970-01-01 00:00:00的秒数
now.getEpochSecond()
完整测试代码
Scanner sc = new Scanner(System.in);
Instant now1 = Instant.now();
System.out.println("请随便输个东西:");
//输入间隔的时间模拟程序运行
int pr = sc.nextInt();
Instant now2 = Instant.now();
System.out.println(now1.getEpochSecond());
System.out.println(now2.getEpochSecond());
long carryTime = now2.getEpochSecond() - now1.getEpochSecond();
System.out.println(carryTime);
运行可得到程序运行的秒数