/**
* 获取本周的所有日期
*/
public List<String> getDats(Integer year, Integer month, Integer day) {
//获取日历实例,默认日期时间为当前,可根据具体的业务场景修改日期时间
Calendar calendar = Calendar.getInstance();
//设置日期为2020-04-08(武汉解封的日子)
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, day);
/**
* 设置日历日期为本周的周一
* 循环取当前的星期和周一做比对,如果不是周一,设置日历往过去推一天,直到设置周一成功
*/
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
calendar.add(Calendar.DAY_OF_MONTH, -1);
}
//当前周日期数组
String[] currentWeekDays = new String[7];
//日期格式化
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
currentWeekDays[0] = df.format(calendar.getTime());
for (int i = 1; i < currentWeekDays.length; i++) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
currentWeekDays[i] = df.format(calendar.getTime());
}
//打印当前周所有日期
// System.out.println(Arrays.asList(currentWeekDays));
List<String> strings = Arrays.asList(currentWeekDays);
return strings;
}
/**
* 获取本周的所有日期
*/
public List<String> getDats(Integer year, Integer month, Integer day) {
//获取日历实例,默认日期时间为当前,可根据具体的业务场景修改日期时间
Calendar calendar = Calendar.getInstance();
//设置日期为2020-04-08(武汉解封的日子)
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, day);
/**
* 设置日历日期为本周的周一
* 循环取当前的星期和周一做比对,如果不是周一,设置日历往过去推一天,直到设置周一成功
*/
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
calendar.add(Calendar.DAY_OF_MONTH, -1);
}
//当前周日期数组
String[] currentWeekDays = new String[7];
//日期格式化
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
currentWeekDays[0] = df.format(calendar.getTime());
for (int i = 1; i < currentWeekDays.length; i++) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
currentWeekDays[i] = df.format(calendar.getTime());
}
//打印当前周所有日期
// System.out.println(Arrays.asList(currentWeekDays));
List<String> strings = Arrays.asList(currentWeekDays);
return strings;
}