java 季度_获取当前时间近8个季度
时间: 2023-07-05 19:24:27 浏览: 162
可以使用Java中的Calendar类和SimpleDateFormat类来获取当前时间近8个季度的日期。
以下是一个示例代码:
```java
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
// 获取当前时间
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 获取当前季度
int quarter = (calendar.get(Calendar.MONTH) / 3) + 1;
// 获取当前时间近8个季度的日期
for (int i = 0; i < 8; i++) {
// 计算季度
int tempQuarter = quarter - i;
int year = calendar.get(Calendar.YEAR);
if (tempQuarter <= 0) {
tempQuarter += 4;
year--;
}
// 计算季度的起始日期和结束日期
calendar.set(year, (tempQuarter - 1) * 3, 1);
String startDate = sdf.format(calendar.getTime());
calendar.set(Calendar.MONTH, (tempQuarter - 1) * 3 + 2);
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DATE, lastDay);
String endDate = sdf.format(calendar.getTime());
// 输出季度的起始日期和结束日期
System.out.println("第" + tempQuarter + "季度:" + startDate + " - " + endDate);
}
}
}
```
运行以上代码将会输出当前时间近8个季度的日期,例如:
```
第1季度:2022-01-01 - 2022-03-31
第4季度:2020-10-01 - 2020-12-31
第3季度:2020-07-01 - 2020-09-30
第2季度:2020-04-01 - 2020-06-30
第1季度:2020-01-01 - 2020-03-31
第4季度:2019-10-01 - 2019-12-31
第3季度:2019-07-01 - 2019-09-30
第2季度:2019-04-01 - 2019-06-30
```
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)