java8计算时间差的几种代码
时间: 2024-02-19 21:33:54 浏览: 24
1. 使用Instant类
```java
Instant start = Instant.now();
// 代码逻辑
Instant end = Instant.now();
Duration duration = Duration.between(start, end);
long seconds = duration.getSeconds();
```
2. 使用LocalDateTime类
```java
LocalDateTime start = LocalDateTime.now();
// 代码逻辑
LocalDateTime end = LocalDateTime.now();
Duration duration = Duration.between(start, end);
long seconds = duration.getSeconds();
```
3. 使用System.currentTimeMillis()
```java
long start = System.currentTimeMillis();
// 代码逻辑
long end = System.currentTimeMillis();
long seconds = (end - start) / 1000;
```
4. 使用Date类
```java
Date start = new Date();
// 代码逻辑
Date end = new Date();
long seconds = (end.getTime() - start.getTime()) / 1000;
```