java定义一个工具方法返回上一季度结束那天最后的时间戳
时间: 2023-12-19 15:14:09 浏览: 110
可以使用以下代码定义一个工具方法来返回上一季度结束那天最后的时间戳:
```java
public static long getLastQuarterEndDayLastTimestamp() {
Calendar calendar = Calendar.getInstance();
int currentMonth = calendar.get(Calendar.MONTH) + 1;
int quarterEndMonth = ((currentMonth - 1) / 3) * 3; // 上一季度结束月份
int quarterEndDay = 1; // 上一季度结束日期为1号
if (quarterEndMonth == 0) { // 如果上一季度结束月份为0,则表示上一季度结束时间为去年的10月1号
quarterEndMonth = 10;
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1);
}
calendar.set(Calendar.MONTH, quarterEndMonth - 1);
calendar.set(Calendar.DAY_OF_MONTH, quarterEndDay);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
long endOfDayTimestamp = calendar.getTimeInMillis() / 1000; // 当天最后一刻时间戳
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long startOfDayTimestamp = calendar.getTimeInMillis() / 1000; // 当天第一刻时间戳
return endOfDayTimestamp - startOfDayTimestamp; // 返回当天最后一刻时间戳
}
```
在上述代码中,我们仍然是使用了 `java.util.Calendar` 类来计算上一季度结束的时间。具体来说,我们首先获取当前时间的月份,然后计算出上一季度结束的月份。如果上一季度结束的月份为0,则表示上一季度结束时间为去年的10月1号。
接下来,我们使用 `Calendar` 对象设置上一季度结束的日期、时间,并将其转换为时间戳(单位为秒)。然后,我们将时间设置为当天的最后一刻时间,并将其转换为时间戳,再将时间设置为当天的第一刻时间,并将其转换为时间戳。最后,我们将当天最后一刻时间戳减去当天第一刻时间戳,即可得到上一季度结束那天最后的时间戳。
阅读全文
相关推荐
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)