java获取当前日期所在周的 周一 周二周三周四周五周六周日
时间: 2023-08-30 14:09:55 浏览: 237
您可以使用Java中的Calendar类来实现获取当前日期所在周的周一、周二、周三、周四、周五、周六和周日的操作。具体代码如下:
```java
import java.util.Calendar;
public class GetWeekdays {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int days = 0;
switch (dayOfWeek) {
case Calendar.MONDAY:
days = 0;
break;
case Calendar.TUESDAY:
days = -1;
break;
case Calendar.WEDNESDAY:
days = -2;
break;
case Calendar.THURSDAY:
days = -3;
break;
case Calendar.FRIDAY:
days = -4;
break;
case Calendar.SATURDAY:
days = -5;
break;
case Calendar.SUNDAY:
days = -6;
break;
}
calendar.add(Calendar.DAY_OF_WEEK, days);
System.out.println("周一:" + calendar.getTime());
calendar.add(Calendar.DAY_OF_WEEK, 1);
System.out.println("周二:" + calendar.getTime());
calendar.add(Calendar.DAY_OF_WEEK, 1);
System.out.println("周三:" + calendar.getTime());
calendar.add(Calendar.DAY_OF_WEEK, 1);
System.out.println("周四:" + calendar.getTime());
calendar.add(Calendar.DAY_OF_WEEK, 1);
System.out.println("周五:" + calendar.getTime());
calendar.add(Calendar.DAY_OF_WEEK, 1);
System.out.println("周六:" + calendar.getTime());
calendar.add(Calendar.DAY_OF_WEEK, 1);
System.out.println("周日:" + calendar.getTime());
}
}
```
输出结果如下:
```
周一:Mon Sep 06 15:23:39 CST 2021
周二:Tue Sep 07 15:23:39 CST 2021
周三:Wed Sep 08 15:23:39 CST 2021
周四:Thu Sep 09 15:23:39 CST 2021
周五:Fri Sep 10 15:23:39 CST 2021
周六:Sat Sep 11 15:23:39 CST 2021
周日:Sun Sep 12 15:23:39 CST 2021
```
希望能够帮助到您!
阅读全文