javaa中如何获取到本周的周一和周日的时间戳
时间: 2023-02-02 10:42:18 浏览: 198
java 获取当前日期时间和本周的星期一的日期时间.docx
可以使用Java中的Calendar类来获取本周的周一和周日的时间戳:
1. 获取当前时间:
```
Calendar calendar = Calendar.getInstance();
```
2. 设置为本周的周一:
```
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
```
3. 将时间设置为0点:
```
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
```
4. 获取周一的时间戳:
```
long weekStart = calendar.getTimeInMillis();
```
5. 设置为本周的周日:
```
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
```
6. 将时间设置为23:59:59:
```
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
```
7. 获取周日的时间戳:
```
long weekEnd = calendar.getTimeInMillis();
```
阅读全文