java处理时间-去除节假日以及双休日
短信预约 -IT技能 免费直播动态提醒
文章目录
有些场景需要计算数据非工作日的情况,eg:统计每个人每月工作日签到情况等,就需要去除法定节假日和工作日,可以新建一个表用来存储和维护这些非工作日。
一、建表:activity_holiday_info
CREATE TABLE `activity_holiday_info` ( `holiday_id` int NOT NULL AUTO_INCREMENT, `holiday_name` varchar(20) DEFAULT NULL COMMENT '节假日名称', `holiday_time` date DEFAULT NULL COMMENT '节假日时间', `remark` varchar(50) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`holiday_id`)) ENGINE=InnoDB COMMENT='节假日';
二、java代码
- 可以先按照年月初始化周末数据入库 initHolidays();
- 按照年份查询已录入系统的非工作日给前端,标记在日历📅中回显展示,供运营人员查看和修改 queryHolidays()。
- 特殊节假日带官方发布该年份后,人工从前端日历控件去修改维护 mergeHolidays()。
1、ActivitityHolidayController.java
@RestController@RequestMapping("/holidays")@Slf4j@Api(tags = "非工作日维护")public class ActivitityHolidayController { @Autowired private ActivityHolidayInfoService activityHolidayInfoService; @ApiOperation(value = "按年份初始化周末") @GetMapping(value = "/{year}/initDate") @ApiImplicitParams({ @ApiImplicitParam(name = "year", dataType = "String", paramType = "path") }) public Result<Boolean> initHolidays(@PathVariable String year) { activityHolidayInfoService.initHolidays(year); return Result.success(Boolean.TRUE); } @ApiOperation(value = "查询已录入系统的非工作日") @GetMapping(value = "/{year}/list") @ApiImplicitParams({ @ApiImplicitParam(name = "year", dataType = "String", paramType = "path") }) public Result<List<ActivityHolidayInfo>> queryHolidays(@PathVariable String year) { return Result.success(activityHolidayInfoService.queryHolidays(year)); } @ApiOperation(value = "更新该年份的非工作日") @PutMapping(value = "/{year}/list") @ApiImplicitParams({ @ApiImplicitParam(name = "year", dataType = "String", paramType = "path") }) public Result<Boolean> mergeHolidays(@RequestBody List<ActivityHolidayInfo> holidayInfos, @PathVariable String year) { activityHolidayInfoService.mergeHolidays(holidayInfos, year); return Result.success(Boolean.TRUE); }}
2、ActivityHolidayInfoService.java
public interface ActivityHolidayInfoService extends IService<ActivityHolidayInfo> { void initHolidays(String year); List<ActivityHolidayInfo> queryHolidays(String year); void mergeHolidays(List<ActivityHolidayInfo> holidayInfos, String year);}
3、ActivityHolidayInfoServiceImpl.java
@Servicepublic class ActivityHolidayInfoServiceImpl extends ServiceImpl<ActivityHolidayInfoMapper, ActivityHolidayInfo> implements ActivityHolidayInfoService { @Override public List<ActivityHolidayInfo> queryHolidays(String year) { QueryWrapper<ActivityHolidayInfo> queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().likeRight(ActivityHolidayInfo::getHolidayTime, year); return baseMapper.selectList(queryWrapper); } @Override public void mergeHolidays(List<ActivityHolidayInfo> holidayInfos, String year) { remove(new QueryWrapper<ActivityHolidayInfo>() .lambda() .likeRight(ActivityHolidayInfo::getHolidayTime, year)); saveBatch(holidayInfos); } // 根据年份和月份获取当月的所有日期 public static List<String> getDayByMonth(int month, String year) { List<String> data = new ArrayList<>(); try { Calendar c = Calendar.getInstance(); // 获取当前的年份 // int year = c.get(Calendar.YEAR); // 获取本月的总天数 int dayCount = c.getActualMaximum(Calendar.DAY_OF_MONTH); // 定义时间格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 开始日期为当前年月拼接1号 Date startDate = sdf.parse(year + "-" + month + "-01"); // 结束日期为当前年月拼接该月最大天数 Date endDate = sdf.parse(year + "-" + month + "-" + dayCount); // 设置calendar的开始日期 c.setTime(startDate); // 当前时间小于等于设定的结束时间 while (c.getTime().compareTo(endDate) <= 0) { String time = sdf.format(c.getTime()); data.add(time); // 当前日期加1 c.add(Calendar.DATE, 1); } } catch (ParseException e) { e.printStackTrace(); } return data; } @Override public void initHolidays(String year) { try { // 拿到当年中的所有日期 List<String> dateList = new ArrayList<>(); for (int i = 1; i <= 12; i++) { dateList.addAll(getDayByMonth(i, year)); } dateList = dateList.stream().distinct().collect(Collectors.toList()); // set 日期 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); List<ActivityHolidayInfo> activityHolidayInfo = new ArrayList<>(); // 添加当年所有日期数据 for (String date : dateList) { ActivityHolidayInfo serviceDate = new ActivityHolidayInfo(); Calendar calendar = Calendar.getInstance(); calendar.setTime(simpleDateFormat.parse(date)); // index 值为 7 时 是周六 值为 1 时是末, 美国周六是一周的最后一天,周日是一周的最后一天 int index = calendar.get(Calendar.DAY_OF_WEEK); if (index == 7) { serviceDate.setHolidayName("周六"); serviceDate.setRemark("周末"); serviceDate.setHolidayTime(simpleDateFormat.parse(date)); activityHolidayInfo.add(serviceDate); } else if (index == 1) { serviceDate.setHolidayName("周日"); serviceDate.setRemark("周末"); serviceDate.setHolidayTime(simpleDateFormat.parse(date)); activityHolidayInfo.add(serviceDate); } } if (!CollectionUtils.isEmpty(activityHolidayInfo)) { //先删除该年份数据 remove(new QueryWrapper<ActivityHolidayInfo>() .lambda() .likeRight(ActivityHolidayInfo::getHolidayTime, year)); //再批量插入 saveBatch(activityHolidayInfo); } } catch (Exception e) { log.error("日期初始化错误"); e.printStackTrace(); } }}
三、测试效果
postman请求:http://x.x.x.x:8080/holidays/年份/initDate
数据库情况:
来源地址:https://blog.csdn.net/weixin_47061482/article/details/133775450
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341