使用java返回string的当前季度第一天和最后一天
时间: 2023-12-24 12:05:34 浏览: 124
以下是使用Java返回当前季度的第一天和最后一天的代码:
```java
import java.time.LocalDate;
import java.time.Month;
public class Main {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate startOfQuarter;
LocalDate endOfQuarter;
switch (today.getMonth()) {
case JANUARY:
case FEBRUARY:
case MARCH:
startOfQuarter = LocalDate.of(today.getYear(), Month.JANUARY, 1);
endOfQuarter = LocalDate.of(today.getYear(), Month.MARCH, 31);
break;
case APRIL:
case MAY:
case JUNE:
startOfQuarter = LocalDate.of(today.getYear(), Month.APRIL, 1);
endOfQuarter = LocalDate.of(today.getYear(), Month.JUNE, 30);
break;
case JULY:
case AUGUST:
case SEPTEMBER:
startOfQuarter = LocalDate.of(today.getYear(), Month.JULY, 1);
endOfQuarter = LocalDate.of(today.getYear(), Month.SEPTEMBER, 30);
break;
default:
startOfQuarter = LocalDate.of(today.getYear(), Month.OCTOBER, 1);
endOfQuarter = LocalDate.of(today.getYear(), Month.DECEMBER, 31);
break;
}
String startOfQuarterString = startOfQuarter.toString();
String endOfQuarterString = endOfQuarter.toString();
System.out.println("当前季度的第一天:" + startOfQuarterString);
System.out.println("当前季度的最后一天:" + endOfQuarterString);
}
}
```
该代码会输出当前季度的第一天和最后一天的字符串形式。例如今天是2021年5月1日,则输出:
```
当前季度的第一天:2021-04-01
当前季度的最后一天:2021-06-30
```
阅读全文