Python 官方文档:入门教程 => 点击学习
文章目录 一、建表:activity_holiday_info二、java代码1、ActivitityHolidayController.java2、ActivityHolidayInfoSe
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='节假日';
@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); }}
public interface ActivityHolidayInfoService extends IService<ActivityHolidayInfo> { void initHolidays(String year); List<ActivityHolidayInfo> queryHolidays(String year); void mergeHolidays(List<ActivityHolidayInfo> holidayInfos, String year);}
@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
--结束END--
本文标题: java处理时间-去除节假日以及双休日
本文链接: https://lsjlt.com/news/428808.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0