写一段当前时间到当前季度结束的秒时间差Java代码
时间: 2023-07-04 17:04:56 浏览: 80
下面是一段计算当前时间到当前季度结束的时间差的Java代码:
```java
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
// 获取当前日期
LocalDate today = LocalDate.now();
// 获取当前季度的结束日期
int month = today.getMonthValue();
LocalDate quarterEnd;
if (month >= 1 && month <= 3) {
quarterEnd = LocalDate.of(today.getYear(), 3, 31);
} else if (month >= 4 && month <= 6) {
quarterEnd = LocalDate.of(today.getYear(), 6, 30);
} else if (month >= 7 && month <= 9) {
quarterEnd = LocalDate.of(today.getYear(), 9, 30);
} else {
quarterEnd = LocalDate.of(today.getYear(), 12, 31);
}
// 计算时间差
long secondsDiff = ChronoUnit.SECONDS.between(today.atStartOfDay(), quarterEnd.atStartOfDay());
System.out.println("当前时间到本季度结束还有 " + secondsDiff + " 秒");
}
}
```
该代码会根据当前日期计算出当前季度的结束日期,并计算时间差,最后输出时间差的秒数。
阅读全文