日历格式输出
- 日历数据列表
- 导出封装日历格式
- 实体类
- 效果
日历数据列表
/**** 封装日历数据* @param year 年份* @param month 月份*/public List<InspectionDailyStaffPlanCalendarData> selectCalendarDataList(int year,int month,List<InspectionDailyStaffPlan> staffPlansList) {//根据日期创建对象,默认当月第一天LocalDate of = LocalDate.of(year, month, 1);//获取当月第一天是周几int valueOfWeek = of.getDayOfWeek().getValue();//判断是否闰年(闰年2月有29天)boolean leapYear = of.isLeapYear();//获取输入月份共有多少天int monthDay = of.getMonth().length(leapYear);List<InspectionDailyStaffPlanCalendarData> list = new ArrayList<>();int weekNum = valueOfWeek;LocalDate today = LocalDate.now(); // 获取今天的日期//打印每个月的日期数for (int i = 1; i <= monthDay; i++) {InspectionDailyStaffPlanCalendarData calendarData = new InspectionDailyStaffPlanCalendarData();calendarData.setDay(i);calendarData.setYear(year);calendarData.setMonth(month);String dateStr = year+"-"+month+"-"+(i<10?"0"+i:i);calendarData.setPlanDate(DateUtils.getDate(dateStr));LocalDate otherDate = LocalDate.of(year, month, i);//判断是否是今日if (today.equals(otherDate)) {calendarData.setToday(1);} else {calendarData.setToday(0);}calendarData.setWeek(WeekEnums.getName(weekNum));if ((i+valueOfWeek-1) % 7 == 0){System.out.println();weekNum = 1;}else{weekNum = weekNum+1;}if(CollUtil.isNotEmpty(staffPlansList)){//封装值班领导和值班人员for (InspectionDailyStaffPlan staffPlan:staffPlansList) {if(staffPlan.getPlanDate().equals(calendarData.getPlanDate())){calendarData.setId(staffPlan.getId());calendarData.setStaffLeaderName(staffPlan.getStaffUserLNames());calendarData.setStaffUserNames(staffPlan.getStaffUserMNames());}}}list.add(calendarData);}return list;}
导出封装日历格式
@Override@SneakyThrowspublic void exportCalendarData(InspectionDailyStaffPlan query) {Date planDate = query.getPlanDate();List<InspectionDailyStaffPlanCalendarData> list = selectCalendarDataList(query);if(CollUtil.isEmpty(list)){throw new BusinessException("导出数据为空.");}ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletResponse response = requestAttributes.getResponse();response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系String fileName = URLEncoder.encode("每月排班记录" + DateUtil.today(), StandardCharsets.UTF_8.toString());response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");EasyExcel.write(response.getOutputStream()).head(StaffPlanCalendarExcelData.head()).sheet().doWrite(StaffPlanCalendarExcelData.getCalendarData(planDate,list));}/**** 导出表头封装* @param* @return*/public static List<List<String>> head() {List<List<String>> headTitles = ListUtils.newArrayList();headTitles.add(ListUtils.newArrayList("星期一"));headTitles.add(ListUtils.newArrayList("星期二"));headTitles.add(ListUtils.newArrayList("星期三"));headTitles.add(ListUtils.newArrayList("星期四"));headTitles.add(ListUtils.newArrayList("星期五"));headTitles.add(ListUtils.newArrayList("星期六"));headTitles.add(ListUtils.newArrayList("星期日"));return headTitles;}/**** 封装日历数据* dataList 封装好的日历列表*/public static List<List<Object>> getCalendarData(Date planDate, List<InspectionDailyStaffPlanCalendarData> dataList) {LocalDate localDate = planDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();int year = localDate.getYear();int month = localDate.getMonthValue();List<List<Object>> contentList = ListUtils.newArrayList();if (CollUtil.isEmpty(dataList)) {return contentList;}//根据日期创建对象,默认当月第一天LocalDate of = LocalDate.of(year, month, 1);//获取当月第一天是周几int valueOfWeek = of.getDayOfWeek().getValue();List<Object> data = new ArrayList<>();for (int i = 1; i < valueOfWeek; i++) {data.add("");}//判断是否闰年(闰年2月有29天)boolean leapYear = of.isLeapYear();//获取输入月份共有多少天int monthDay = of.getMonth().length(leapYear);//打印每个月的日期数int weekNum = 1;for (int i = 1; i <= monthDay; i++) {if ((i+valueOfWeek-1) % 7 == 0){weekNum++;}}//周int dayOfWeek = 7-valueOfWeek;int val = 0;for (int y = 1; y <= weekNum; y++) {List<Object> obj = new ArrayList<>();for (int i = 1; i <=7; i++) {String str = "";if(dataList.size()>0){InspectionDailyStaffPlanCalendarData calendarData = dataList.get(0);str = "\t\t"+ calendarData.getDay()+"\n";String lNames = StringUtils.isNoneBlank(calendarData.getStaffLeaderName())?"值班领导:"+calendarData.getStaffLeaderName()+"\n":"";String mNames = StringUtils.isNoneBlank(calendarData.getStaffUserNames())?"值班人员:"+calendarData.getStaffUserNames():"";str = "\t\t"+ calendarData.getDay()+"\n"+lNames+mNames;dataList.remove(calendarData);}if(dayOfWeek>=0){dayOfWeek--;data.add(str);}else{obj.add(str);}val++;if ((val+valueOfWeek-1) % 7 == 0){break;}}if(y == 1){contentList.add(data);}else{contentList.add(obj);}}return contentList;}
实体类
public class InspectionDailyStaffPlanCalendarData
{private static final long serialVersionUID = 1L;/** 主键id */private Long id;private int year;private int month;private int day;/** 值班计划日期 */@JsonFormat(pattern = "yyyy-MM-dd")private Date planDate;/** 星期 */private String week;/** 值班领导 */private String staffLeaderName;/** 值班人员 */private String staffUserNames;/** 是否今日 1:是 */private int today;}
效果
日历列表图:
导出数据: