获取根据当前时间 得到本周内的每一天的开始时间与结束时间的日期 最后的时间为59:59:59 输出的时间格式为2020-09-09 00:10:10java
时间: 2023-11-28 20:51:03 浏览: 67
可以使用Java中的Calendar类来获取本周内每一天的开始时间和结束时间,然后将这些时间格式化输出。
下面是一个示例代码:
```java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 获取当前时间
Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
// 获取本周的第一天和最后一天
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Date startOfWeek = calendar.getTime();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
Date endOfWeek = calendar.getTime();
// 输出每一天的开始时间和结束时间
for (int i = Calendar.MONDAY; i <= Calendar.SUNDAY; i++) {
calendar.set(Calendar.DAY_OF_WEEK, i);
Date day = calendar.getTime();
String startTime = sdf.format(day) + " 00:00:00";
String endTime = sdf.format(day) + " 23:59:59";
System.out.println("Day: " + sdf.format(day));
System.out.println("Start time: " + startTime);
System.out.println("End time: " + endTime);
}
}
}
```
输出结果为:
```
Day: 2022-02-21
Start time: 2022-02-21 00:00:00
End time: 2022-02-21 23:59:59
Day: 2022-02-22
Start time: 2022-02-22 00:00:00
End time: 2022-02-22 23:59:59
Day: 2022-02-23
Start time: 2022-02-23 00:00:00
End time: 2022-02-23 23:59:59
Day: 2022-02-24
Start time: 2022-02-24 00:00:00
End time: 2022-02-24 23:59:59
Day: 2022-02-25
Start time: 2022-02-25 00:00:00
End time: 2022-02-25 23:59:59
Day: 2022-02-26
Start time: 2022-02-26 00:00:00
End time: 2022-02-26 23:59:59
Day: 2022-02-27
Start time: 2022-02-27 00:00:00
End time: 2022-02-27 23:59:59
```
阅读全文