获取当年的周末 日期 并入库存放

直接上干货  根据传入的年份 获取全年的周末时间 周六和周日 


public static List<Date> getAllWeekendDay(String newYear) {
List<Date> strAllWeekendDayList = new ArrayList<>();
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyy-MM-dd");
Calendar date = Calendar.getInstance();
       //当年年份 如果没有传值 默认当年
String currentYear = String.valueOf(date.get(Calendar.YEAR));
String year = StringUtils.isEmpty(newYear) ? currentYear : newYear;
Calendar c = Calendar.getInstance();
c.set(Integer.parseInt(year), 0, 1);
Calendar c2 = Calendar.getInstance();
c2.set(Integer.parseInt(year) + 1, 0, 1);
while (c.compareTo(c2) < 0) {
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
String weekendDay = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
+ c.get(Calendar.DAY_OF_MONTH);
// strAllWeekendDayList.add(weekendDay);
strAllWeekendDayList.add(simpleDateFormat.parse(weekendDay));
System.out.println(c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
+ c.get(Calendar.DAY_OF_MONTH));
}
// 日期+1
c.add(Calendar.DATE, 1);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return strAllWeekendDayList;
}

根据日期 直接获取日期是周六还是周日 文章描述
//需要用到SimpleDateFormat 中的 EEEE
SimpleDateFormat sim1=new SimpleDateFormat("EEEE");
List<Date> dateWeekDayList = DateUtils.getAllWeekendDay(currentYear);
if (!CollectionUtil.isEmpty(dateWeekDayList)) {
dateWeekDayList.forEach(e->{
Weekday weekday = new Weekday();
weekday.setYear(Integer.parseInt(currentYear));
weekday.setDate(e);
  //周六 周日 描述字段维护
weekday.setDesc(sim1.format(e));
weekdayMapper.insertSelective(weekday);
});
}

mysql 数据库

CREATE TABLE `weekday` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`desc` varchar(200) DEFAULT NULL COMMENT '描述',
`year` int(5) DEFAULT NULL COMMENT '年份',
`date` date NOT NULL COMMENT '日期',
`is_holiday` int(1) DEFAULT '0' COMMENT '是否为国定假日(0:否;1:是)',
`is_work` int(1) DEFAULT '0' COMMENT '是否补班(0:否;1:是)',
PRIMARY KEY (`id`),
UNIQUE KEY `date` (`date`)
) ENGINE=InnoDB AUTO_INCREMENT=524 DEFAULT CHARSET=utf8 COMMENT='周末信息表';

数据展示




原文地址:https://www.cnblogs.com/xzcBY/p/15190826.html