运行结果需要取今天的时间,用户体验才会更好

动态 未结 0 123
小小草
小小草 LV1 2023年9月23日 23:40 发表
如果需要获取本周的开始时间和结束时间,并且始终以当天为基准,可以使用以下代码: ``` import java.time.DayOfWeek; import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { // 获取当天日期 LocalDate today = LocalDate.now(); // 获取当天所在周的第一天(周一) LocalDate firstDayOfWeek = today.with(DayOfWeek.MONDAY); // 定义日期格式 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 获取当天所在周的最后一天(周日) LocalDate lastDayOfWeek = firstDayOfWeek.plusDays(6); // 格式化输出结果 String firstDay = firstDayOfWeek.format(formatter); String lastDay = lastDayOfWeek.format(formatter); System.out.println("本周开始时间:" + firstDay); System.out.println("本周结束时间:" + lastDay); } } ``` 运行结果会根据当天日期而变化。例如,如果今天是2022年3月1日,则运行结果如下: ``` 本周开始时间:2022-02-28 本周结束时间:2022-03-06 ``` 这样可以保证用户体验更好。
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复