本文主要是介绍java处理时间-去除节假日以及双休日,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 一、建表:activity_holiday_info
- 二、java代码
- 1、ActivitityHolidayController.java
- 2、ActivityHolidayInfoService.java
- 3、ActivityHolidayInfoServiceImpl.java
- 三、测试效果
有些场景需要计算数据非工作日的情况,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
/*** @author qy* 非工作日维护*/
@RestController
@RequestMapping("/holidays")
@Slf4j
@Api(tags = "非工作日维护")
public class ActivitityHolidayController {@Autowiredprivate 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
@Service
public class ActivityHolidayInfoServiceImpl extends ServiceImpl<ActivityHolidayInfoMapper, ActivityHolidayInfo> implements ActivityHolidayInfoService {/*** 查询改年份已录入系统的非工作日*/@Overridepublic List<ActivityHolidayInfo> queryHolidays(String year) {QueryWrapper<ActivityHolidayInfo> queryWrapper = new QueryWrapper<>();queryWrapper.lambda().likeRight(ActivityHolidayInfo::getHolidayTime, year);return baseMapper.selectList(queryWrapper);}/*** 更新该年份的非工作日*/@Overridepublic 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);// 当前日期加1c.add(Calendar.DATE, 1);}} catch (ParseException e) {e.printStackTrace();}return data;}/*** 初始化当年周六、周末*/@Overridepublic 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
数据库情况:
这篇关于java处理时间-去除节假日以及双休日的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!