java 当前时间获取过去4个季度时间
时间: 2024-06-14 10:06:20 浏览: 165
获取当前月份所属季度之前的四个季度(可以获取多个)
4星 · 用户满意度95%
```java
import java.time.LocalDate;
import java.time.Month;
public class Main {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
int currentYear = currentDate.getYear();
Month currentMonth = currentDate.getMonth();
for (int i = 0; i < 4; i++) {
Month quarterStartMonth = currentMonth.minus(3 * i);
int quarterStartYear = currentYear; if (quarterStartMonth.getValue() <= 0) {
quarterStartMonth = quarterStartMonth.plus(12);
quarterStartYear--;
}
LocalDate quarterStartDate = LocalDate.of(quarterStartYear, quarterStartMonth, 1);
LocalDate quarterEndDate = quarterStartDate.plusMonths(3).minusDays(1);
System.out.println("Quarter " + (i + 1) + " start date: " + quarterStartDate);
System.out.println("Quarter " + (i + 1) + " end date: " + quarterEndDate);
}
}
}
```
阅读全文