hutool获取指定时间周几
@Slf4j
public class DateWeekUtil {public static List<String> getDateWeek(String startDateString, String endDateString, List<Integer> codeList) {List<String> dateInfoList = new ArrayList<>();List<Integer> valueList = WeekTypeEnum.getValueByCode(codeList);List<String> dateStrList = getDatesBetween(startDateString, endDateString);for (String dateStr : dateStrList) {Date date = parseYyyyMMdd(dateStr);Integer weekDay = DateUtil.dayOfWeek(date);//获得指定日期是星期几,1表示周日,2表示周一if (valueList.contains(weekDay)) {dateInfoList.add(dateStr);}}return dateInfoList;}/*** 输入输出yyyy-MM-dd格式** @param startDateString* @param endDateString* @return*/public static List<String> getDatesBetween(String startDateString, String endDateString) {List<DateTime> datesBetween = DateUtil.rangeToList(DateUtil.parseDate(startDateString), DateUtil.parseDate(endDateString), DateField.DAY_OF_MONTH);return datesBetween.stream().map(p -> DateUtil.format(p, DatePattern.NORM_DATE_PATTERN)).collect(Collectors.toList());}/*** 输入输出yyyy-MM-dd格式** @param startDateString* @param endDateString* @return*/public static List<DateItemVo> getDateItemVoBetween(String startDateString, String endDateString) {List<DateTime> datesBetween = DateUtil.rangeToList(DateUtil.parseDate(startDateString), DateUtil.parseDate(endDateString), DateField.DAY_OF_MONTH);List<String> dateInfoList = datesBetween.stream().map(p -> DateUtil.format(p, DatePattern.NORM_DATE_PATTERN)).collect(Collectors.toList());List<DateItemVo> dateItemVoList = new ArrayList<>();for (String dateInfo : dateInfoList) {String[] split = dateInfo.split("-");int year = Integer.parseInt(split[0]);int mounth = Integer.parseInt(split[1]);int day = Integer.parseInt(split[2]);Date dateTime = parseYyyyMMdd(dateInfo);Integer weekDayValue = DateUtil.dayOfWeek(dateTime);//获得指定日期是星期几,1表示周日,2表示周一DateItemVo dateItemVo = new DateItemVo();dateItemVo.setTimeInfo(dateInfo);dateItemVo.setTimeInfoShort(mounth + "." + day);dateItemVo.setWeekCode(WeekTypeEnum.getWeekTypeEnumByValue(weekDayValue).getCode());dateItemVo.setWeekName(WeekTypeEnum.getWeekTypeEnumByValue(weekDayValue).getName());dateItemVo.setWeekValue(weekDayValue);dateItemVoList.add(dateItemVo);}return dateItemVoList;}public static DateItemVo getToday(Date todayDate) {String todayDateString = DateUtil.format(todayDate, "yyyy-MM-dd");String[] split = todayDateString.split("-");int year = Integer.parseInt(split[0]);int mounth = Integer.parseInt(split[1]);int day = Integer.parseInt(split[2]);Integer weekDayValue = DateUtil.dayOfWeek(todayDate);//获得指定日期是星期几,1表示周日,2表示周一DateItemVo dateItemVo = new DateItemVo();dateItemVo.setWeekCode(WeekTypeEnum.getWeekTypeEnumByValue(weekDayValue).getCode());dateItemVo.setWeekName(WeekTypeEnum.getWeekTypeEnumByValue(weekDayValue).getName());dateItemVo.setWeekValue(weekDayValue);dateItemVo.setTimeInfo(todayDateString);dateItemVo.setTimeInfoShort(mounth + "." + day);return dateItemVo;}public static void main(String[] args) {List<Integer> integers = Arrays.asList(1, 2, 6, 7);List<String> datesBetweenList = getDateWeek("2024-02-27", "2024-04-03", integers);List<DateItemVo> dateItemVoBetween = DateWeekUtil.getDateItemVoBetween("2024-02-27", "2024-04-03");DateItemVo today = DateWeekUtil.getToday(new Date());System.out.println();}/*** 日期格式化** @param date yyyy-MM-dd* @return*/public static Date parseYyyyMMdd(String date) {if (StringUtils.isBlank(date)) {return null;}try {return new SimpleDateFormat("yyyy-MM-dd").parse(date);} catch (ParseException e) {e.printStackTrace();throw new RuntimeException(e);}}}
package com.unicom.attendance.base.enums;import java.util.*;
import java.util.stream.Collectors;/*** @Copyright: Unicom (Zhejiang) Industrial Internet Co. <br/>* @Desc: <br/>* @ProjectName: park-hztljb-api <br/>* @Date: 2023/1/16 15:45 <br/>* @Author: March*/
public enum WeekTypeEnum {/*** 1按日期选择 2按周选择*/WEEK1(1, "周一",2),WEEK2(2, "周二",3),WEEK3(3, "周三",4),WEEK4(4, "周四",5),WEEK5(5, "周五",6),WEEK6(6, "周六",7),WEEK7(7, "周日",1);WeekTypeEnum(Integer code, String name,Integer value) {this.code = code;this.name = name;this.value = value;}private Integer code;// 1周一 2周二 3周三 4周四 5周五 6周六 7周日private String name;private Integer value;//hutool定义的 (1=星期日,2=星期一...7=星期六)// int i1 = DateUtil.dayOfWeek(parse1);//获得指定日期是星期几,1表示周日,2表示周一public Integer getCode() {return this.code;}public Integer getValue() {return value;}public String getName() {return this.name;}public static WeekTypeEnum getWeekTypeEnumByCode(Integer codeInfo) {for(WeekTypeEnum weekTypeEnum: values()){if(weekTypeEnum.getCode().equals(codeInfo)){return weekTypeEnum;}}return null;}public static WeekTypeEnum getWeekTypeEnumByValue(Integer valueInfo) {for(WeekTypeEnum weekTypeEnum: values()){if(weekTypeEnum.getValue().equals(valueInfo)){return weekTypeEnum;}}return null;}public static List<Integer> getValueByCode(List<Integer> codeInfoList) {List<WeekTypeEnum> weekTypeEnumList = new ArrayList<>();List<Integer> valueList = new ArrayList<>();for(WeekTypeEnum weekTypeEnum: values()){for(Integer codeInfo:codeInfoList){if(weekTypeEnum.getCode().equals(codeInfo)){weekTypeEnumList.add(weekTypeEnum);}}}valueList= weekTypeEnumList.stream().map(WeekTypeEnum::getValue).collect(Collectors.toList());return valueList;}public static void main(String[] args) {WeekTypeEnum weekTypeEnum1 = getWeekTypeEnumByCode(1);WeekTypeEnum weekTypeEnum3 = getWeekTypeEnumByCode(3);WeekTypeEnum weekTypeEnum7 = getWeekTypeEnumByCode(7);List<Integer> integers = Arrays.asList(1, 3, 6, 7);List<Integer> valueByCode = getValueByCode(integers);System.out.println();}}
输出指定日期区间内的所有天、周、月_hutool获取次月的天数并把每一天输出-CSDN博客