Java实现节日维护
生活随笔
收集整理的這篇文章主要介紹了
Java实现节日维护
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
背景:管理后臺(tái)添加節(jié)日規(guī)則,前端展示節(jié)日
實(shí)現(xiàn)方案設(shè)計(jì)
將所有節(jié)日分類,每類節(jié)日對(duì)應(yīng)一個(gè)時(shí)間計(jì)算規(guī)則,添加節(jié)日時(shí)生成該節(jié)日近50年的時(shí)間,前端查詢節(jié)日時(shí),根據(jù)傳入時(shí)間戳,即可查詢最近節(jié)日。
代碼:
@Getter @AllArgsConstructor public enum FestivalCreateTypeEnum {SOLAR_DAY(1,"陽歷的某月某天"),LUNAR_DAY(2,"陰歷的某月某天"),TWENTY_FOUR(3,"二十四節(jié)氣"),WEEK_DAY(4,"每年某月某周周幾"),LAST_WEEK_DAY(6,"每年某月最后一周周幾"),F_BEFORE(5,"某個(gè)節(jié)日的前幾天");private final int code;private final String desc;private static final Map<Integer, FestivalCreateTypeEnum> map = new HashMap<>();static {for (FestivalCreateTypeEnum typeEnum: FestivalCreateTypeEnum.values()) {map.put(typeEnum.getCode(), typeEnum);}}public static FestivalCreateTypeEnum getEnum(int code){return map.get(code);}} //添加節(jié)日入?yún)?/span> @Data public class AddCalendarDto implements Serializable {/*** 創(chuàng)建類型 1-陽歷每年某月某天,2-農(nóng)歷每年某與某天,* 3-二十四節(jié)氣,4-每年某月某周 周幾,5-某個(gè)節(jié)日的前幾天or后幾天* */private Integer createType;/*** 節(jié)日類型* */private Integer typeId;/*** 節(jié)日名稱* */private String name;/*** 第幾月* */private Integer monthNum;/*** 第幾天* */private Integer dayNum;/*** 第幾周* */private Integer weekCount;/*** 周幾* */private Integer weekNum;/*** 二十四節(jié)氣對(duì)應(yīng)C值* */private Double c;/*** 時(shí)間* */private Long date;/*** 狀態(tài) 0無效 1有效* */private Integer status;/*** 每個(gè)節(jié)氣對(duì)應(yīng)的順序 0-小寒 1大寒 2立春 3雨水 4驚蟄 5春分 6清明 7谷雨 8立夏 9小滿 10芒種 11夏至* 12小暑 13大暑 14立秋 15處暑 16白露 17秋分 18寒露 19霜降 20立冬 21小雪 22大雪 23冬至*/private Integer sort;/*** 時(shí)間類型 0非規(guī)律時(shí)間 1規(guī)律時(shí)間* */private Integer timeType;private String alias;private String desc;private String displayName;private String imageUrl;private String keyWords;private Integer festivalId;private List<Long> dateList; } //添加節(jié)日方法 public ServerResponse addFestival(AddCalendarDto param) {if (param.getTimeType() == null || StringUtils.isBlank(param.getName())) {return ServerResponse.createByError(-1,"參數(shù)錯(cuò)誤");}//判斷是否已經(jīng)有該節(jié)日FestivalDo festivalDo = festivalDao.selectByName(param.getName());if (!Objects.isNull(festivalDo)) {return ServerResponse.createByError(-2,"該節(jié)日已存在");}//自定義節(jié)日if (param.getTimeType() == 0) {if (CollectionUtils.isEmpty(param.getDateList())) {return ServerResponse.createByError(-1,"參數(shù)錯(cuò)誤");}CustomizeFestivalDto customizeFestivalDto = new CustomizeFestivalDto();customizeFestivalDto = (CustomizeFestivalDto)convertFesDto(customizeFestivalDto, param);customizeFestivalDto.setDateList(param.getDateList());customizeFestivalFactory.createFestival(customizeFestivalDto);return ServerResponse.createBySuccess();}//已有規(guī)律性節(jié)日FestivalCreateTypeEnum fTypeEnum = FestivalCreateTypeEnum.getEnum(param.getCreateType());if (Objects.isNull(fTypeEnum)) {return ServerResponse.createByError(-3,"節(jié)日創(chuàng)建類型錯(cuò)誤");}boolean isSuccess = false;switch (fTypeEnum) {case F_BEFORE://依附于某個(gè)節(jié)日-寒食if (param.getFestivalId() == null || param.getDayNum()==0) {break;}BeforeFestivalDto beforeFestivalDto = new BeforeFestivalDto();beforeFestivalDto = (BeforeFestivalDto)convertFesDto(beforeFestivalDto, param);beforeFestivalDto.setFestivalId(param.getFestivalId());beforeFestivalDto.setDayCount(param.getDayNum());isSuccess = beforeFestivalFactory.createFestival(beforeFestivalDto);break;case WEEK_DAY://周幾的節(jié)日-母親節(jié)if (param.getMonthNum()<0 || param.getWeekCount()<0 || param.getWeekNum()<0) {break;}WeekFestivalDto weekFestivalDto = new WeekFestivalDto();weekFestivalDto = (WeekFestivalDto)convertFesDto(weekFestivalDto, param);weekFestivalDto.setMonthNum(param.getMonthNum());weekFestivalDto.setWeekCount(param.getWeekCount());weekFestivalDto.setWeekNum(param.getWeekNum());isSuccess = weekFestivalFactory.createFestival(weekFestivalDto);break;case LUNAR_DAY://農(nóng)歷的節(jié)日-春節(jié)if (param.getMonthNum()<0 || param.getDayNum()<0) {break;}LunarFestivalDto lFestivalDto = new LunarFestivalDto();lFestivalDto = (LunarFestivalDto)convertFesDto(lFestivalDto, param);lFestivalDto.setMonthNum(param.getMonthNum());lFestivalDto.setDayNum(param.getDayNum());isSuccess = lunarFestivalFactory.createFestival(lFestivalDto);break;case SOLAR_DAY://陽歷的節(jié)日-勞動(dòng)節(jié)if (param.getMonthNum() < 0 || param.getDayNum() < 0) {break;}SolarFestivalDto sFestivalDto = new SolarFestivalDto();sFestivalDto = (SolarFestivalDto)convertFesDto(sFestivalDto, param);sFestivalDto.setMonthNum(param.getMonthNum());sFestivalDto.setDayNum(param.getDayNum());isSuccess = solarFestivalFactory.createFestival(sFestivalDto);break;case TWENTY_FOUR://二十四節(jié)氣if (param.getSort() < 0 || param.getC() < 0) {break;}TwentyFourFestivalDto tfParam = new TwentyFourFestivalDto();tfParam = (TwentyFourFestivalDto)convertFesDto(tfParam, param);tfParam.setC(param.getC());tfParam.setSort(param.getSort());tfParam.setName(tfParam.getName());isSuccess = twentyFourFestivalFactory.createFestival(tfParam);break;case LAST_WEEK_DAY://最后一周的周幾-地球一小時(shí)if (param.getMonthNum() < 0 || param.getWeekNum() < 0) {break;}LastWeekFestivalDto lastWeekFestivalDto = new LastWeekFestivalDto();lastWeekFestivalDto = (LastWeekFestivalDto)convertFesDto(lastWeekFestivalDto, param);lastWeekFestivalDto.setMonthNum(param.getMonthNum());lastWeekFestivalDto.setWeekNum(param.getWeekNum());isSuccess = lastWeekFestivalFactory.createFestival(lastWeekFestivalDto);break;default:break;}if (isSuccess) {return ServerResponse.createBySuccess();}return ServerResponse.createByError(-10,"創(chuàng)建節(jié)日失敗");} //陽歷節(jié)日 @Service @Slf4j public class SolarFestivalFactoryImpl implements FestivalFactory {@Autowiredprivate FestivalDao festivalDao;@Autowiredprivate FestivalTimeDao festivalTimeDao;@Overridepublic boolean createFestival(FestivalDto festivalDto) {//創(chuàng)建近3年的節(jié)日SolarFestivalDto solarFestivalDto = (SolarFestivalDto)festivalDto;FestivalDo festivalDo = new FestivalDo();festivalDo = DomainConvertUtils.festivalConvertDo(solarFestivalDto,festivalDo);festivalDo.setMonthNum(solarFestivalDto.getMonthNum());festivalDo.setDayNum(solarFestivalDto.getDayNum());festivalDao.insertSelective(festivalDo);FestivalDo fDo = festivalDao.selectByName(solarFestivalDto.getName());if (Objects.isNull(fDo)) {return false;}List<FestivalTimeDo> festivalTimeDos = Lists.newArrayList();//獲取當(dāng)前年Calendar rightNow = Calendar.getInstance();int year = rightNow.get(Calendar.YEAR);// 獲取當(dāng)前年份for (int i=0;i<50;i++) {FestivalTimeDo festivalTimeDo = new FestivalTimeDo();festivalTimeDo.setStatus(1);festivalTimeDo.setFestivalId(fDo.getId());String str = year+i+"-"+solarFestivalDto.getMonthNum()+"-"+solarFestivalDto.getDayNum();try {Date date = DateUtils.parseDate(str, "yyyy-MM-dd");festivalTimeDo.setTime(date);festivalTimeDos.add(festivalTimeDo);} catch (Exception e) {}}log.info("[SolarFestivalFactoryImpl]festivalTimeDos:{}", JSON.toJSONString(festivalTimeDos));if (CollectionUtils.isNotEmpty(festivalTimeDos)) {festivalTimeDao.batchInsert(festivalTimeDos);}return true;}} //農(nóng)歷節(jié)日 @Service @Slf4j public class LunarFestivalFactoryImpl implements FestivalFactory {@Autowiredprivate FestivalDao festivalDao;@Autowiredprivate FestivalTimeDao festivalTimeDao;@Overridepublic boolean createFestival(FestivalDto festivalDto) {//創(chuàng)建近3年的節(jié)日LunarFestivalDto lunarFestivalDto = (LunarFestivalDto)festivalDto;FestivalDo festivalDo = new FestivalDo();festivalDo = DomainConvertUtils.festivalConvertDo(lunarFestivalDto,festivalDo);festivalDo.setMonthNum(lunarFestivalDto.getMonthNum());festivalDo.setDayNum(lunarFestivalDto.getDayNum());festivalDao.insertSelective(festivalDo);FestivalDo fDo = festivalDao.selectByName(lunarFestivalDto.getName());if (Objects.isNull(fDo)) {return false;}List<FestivalTimeDo> festivalTimeDos = Lists.newArrayList();//獲取當(dāng)前年Calendar rightNow = Calendar.getInstance();int year = rightNow.get(Calendar.YEAR);// 獲取當(dāng)前年份for (int i=0;i<50;i++) {FestivalTimeDo festivalTimeDo = new FestivalTimeDo();festivalTimeDo.setFestivalId(fDo.getId());festivalTimeDo.setStatus(1);//指定陰歷的某一天Lunar luDate = new Lunar(year+i,lunarFestivalDto.getMonthNum(),lunarFestivalDto.getDayNum());festivalTimeDo.setTime(luDate.getSolar().getCalendar().getTime());festivalTimeDos.add(festivalTimeDo);}//批量添加festivalTimeDao.batchInsert(festivalTimeDos);return true;} } //二十四節(jié)氣 @Service @Slf4j public class TwentyFourFestivalFactoryImpl implements FestivalFactory {@Autowiredprivate FestivalDao festivalDao;@Autowiredprivate FestivalTimeDao festivalTimeDao;//二十四節(jié)氣日期偏移度private static final double D = 0.2422;//特殊年份節(jié)氣日期偏移private final static Map<Integer, Integer[]> INCREASE_OFFSETMAP = new HashMap<Integer, Integer[]>();//+1偏移private final static Map<Integer, Integer[]> DECREASE_OFFSETMAP = new HashMap<Integer, Integer[]>();//-1偏移@Overridepublic boolean createFestival(FestivalDto festivalDto) {TwentyFourFestivalDto tfFestivalDto = (TwentyFourFestivalDto) festivalDto;FestivalDo festivalDo = new FestivalDo();festivalDo = DomainConvertUtils.festivalConvertDo(tfFestivalDto,festivalDo);festivalDao.insertSelective(festivalDo);FestivalDo fDo = festivalDao.selectByName(tfFestivalDto.getName());if (Objects.isNull(fDo)) {return false;}List<FestivalTimeDo> festivalTimeDos = Lists.newArrayList();log.info("[TwentyFourFestivalFactoryImpl]tfFestivalDto:{}",tfFestivalDto);//獲取當(dāng)前年Calendar rightNow = Calendar.getInstance();int year = rightNow.get(Calendar.YEAR);for (int i=0;i<50;i++) {Date date = sTerm(year + i, tfFestivalDto.getSort(), tfFestivalDto.getC());if (date == null) {break;}FestivalTimeDo festivalTimeDo = new FestivalTimeDo();festivalTimeDo.setFestivalId(fDo.getId());festivalTimeDo.setStatus(1);festivalTimeDo.setTime(date);festivalTimeDos.add(festivalTimeDo);}log.info("[TwentyFourFestivalFactoryImpl]festivalTimeDos:{}", JSON.toJSONString(festivalTimeDos));if (CollectionUtils.isNotEmpty(festivalTimeDos)) {festivalTimeDao.batchInsert(festivalTimeDos);}return true;}/*** 獲取某年的第n個(gè)節(jié)氣為幾日(從0小寒起算)** @param year* @param n* @param centuryValue 氣的21世紀(jì)C值,每個(gè)節(jié)氣的世紀(jì)值都不同* @return*/private Date sTerm(int year, int n,double centuryValue) {int dateNum = 0;int y = year % 100;//步驟1:取年分的后兩位數(shù)if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {//閏年if (n == 0 || n == 1 || n == 2 || n == 3) {//注意:凡閏年3月1日前閏年數(shù)要減一,即:L=[(Y-1)/4],因?yàn)樾『⒋蠛⒘⒋骸⒂晁@兩個(gè)節(jié)氣都小于3月1日,所以 y = y-1y = y - 1;//步驟2}}//獲取月份int month = getFesMonth(n);dateNum = (int) (y * D + centuryValue) - (int) (y / 4);//步驟3,使用公式[Y*D+C]-L計(jì)算dateNum += specialYearOffset(year, n);//步驟4,加上特殊的年分的節(jié)氣偏移量//獲取當(dāng)前年的第一天String dateStr = year+"-"+month+"-"+01;try {Date date = DateUtils.parseDate(dateStr, "yyyy-MM-dd");Date date1 = DateUtils.addDays(date, dateNum-1);return date1;} catch (Exception e) {}return null;}//獲取每個(gè)節(jié)氣對(duì)應(yīng)的月份private int getFesMonth(int sort) {if (sort>24 || sort<0) {return 0;}if (sort < 2) {//0 1return 1;} else if (1 < sort && sort < 4) {//2 3return 2;} else if (sort>3 && sort<6) {// 4 5return 3;} else if (sort>5 && sort<8) {//6 7return 4;} else if (sort>7 && sort<10) {//8 9return 5;} else if (sort>9 && sort<12) {//10 11return 6;} else if (sort>11 && sort<14) {//12 13return 7;} else if (sort>13 && sort<16) {//14 15return 8;} else if (sort>15 && sort<18) {//16 17return 9;} else if (sort>17 && sort<20) {//18 19return 10;} else if (sort>19 && sort<22) {//20 21return 11;} else if (sort>21 && sort<24) {//22 23return 12;}return 0;}/*** 特例,特殊的年分的節(jié)氣偏移量,由于公式并不完善,所以算出的個(gè)別節(jié)氣的第幾天數(shù)并不準(zhǔn)確,在此返回其偏移量** @param year 年份* @param n 節(jié)氣編號(hào)* @return 返回其偏移量*/private int specialYearOffset(int year, int n) {int offset = 0;offset += getOffset(DECREASE_OFFSETMAP, year, n, -1);offset += getOffset(INCREASE_OFFSETMAP, year, n, 1);return offset;}/*** 節(jié)氣偏移量計(jì)算** @param map* @param year* @param n* @param offset* @return*/private int getOffset(Map<Integer, Integer[]> map, int year, int n, int offset) {int off = 0;Integer[] years = map.get(n);if (null != years) {for (int i : years) {if (i == year) {off = offset;break;}}}return off;}} //周幾的節(jié)日-母親節(jié) @Service("weekFestivalFactory") @Slf4j public class WeekFestivalFactoryImpl implements FestivalFactory {@Autowiredprivate FestivalTimeDao festivalTimeDao;@Autowiredprivate FestivalDao festivalDao;@DubboReferenceTemplateService templateService;@Overridepublic boolean createFestival(FestivalDto festivalDto) {//創(chuàng)建50年的節(jié)日WeekFestivalDto weekFestivalDto = (WeekFestivalDto) festivalDto;FestivalDo festivalDo = new FestivalDo();festivalDo = DomainConvertUtils.festivalConvertDo(weekFestivalDto,festivalDo);festivalDo.setMonthNum(weekFestivalDto.getMonthNum());festivalDo.setWeekCount(weekFestivalDto.getWeekCount());festivalDo.setWeekNum(weekFestivalDto.getWeekNum());festivalDao.insertSelective(festivalDo);FestivalDo fDo = festivalDao.selectByName(weekFestivalDto.getName());if (Objects.isNull(fDo)) {return false;}log.info("[WeekFestivalFactoryImpl]fDo:{}",fDo);List<FestivalTimeDo> festivalTimeDos = Lists.newArrayList();//獲取當(dāng)前年Calendar rightNow = Calendar.getInstance();int year = rightNow.get(Calendar.YEAR);// 獲取當(dāng)前年份for (int i = 0; i < 50; i++) {Date date = getSundayFestival(year + i, weekFestivalDto.getMonthNum(), weekFestivalDto.getWeekCount(), weekFestivalDto.getWeekNum());if (date == null) {break;}FestivalTimeDo festivalTimeDo = new FestivalTimeDo();festivalTimeDo.setTime(date);festivalTimeDo.setFestivalId(fDo.getId());festivalTimeDo.setStatus(1);festivalTimeDos.add(festivalTimeDo);}log.info("[WeekFestivalFactoryImpl]festivalTimeDos:{}", JSON.toJSONString(festivalTimeDos));if (CollectionUtils.isNotEmpty(festivalTimeDos)) {festivalTimeDao.batchInsert(festivalTimeDos);}return true;}/*** 周日的節(jié)日** @param year* @param month* @param weekCount 第幾個(gè)周* @param weekNum 周幾*/private Date getSundayFestival(int year, int month, int weekCount, int weekNum) {//獲取當(dāng)前年的第一天String dateStr = year + "-" + month + "-" + 01;Date date = null;try {date = DateUtils.parseDate(dateStr, "yyyy-MM-dd");} catch (Exception e) {return null;}Calendar cal = Calendar.getInstance();cal.setTime(date);int dayCount = cal.getActualMaximum(Calendar.DAY_OF_MONTH);cal.set(Calendar.YEAR, year);cal.set(Calendar.MONTH, month - 1);int sundays = 0;int mondays = 0;int tuesdays = 0;int wednesdays = 0;int thursdays = 0;int fridays = 0;int saturdays = 0;for (int i = 1; i <= dayCount; i++) {cal.set(Calendar.DATE, i);switch (weekNum) {case 7://周日if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {sundays++;if (sundays == weekCount) {String fDateStr = year + "-" + month + "-" + i;try {Date fDate = DateUtils.parseDate(fDateStr, "yyyy-MM-dd");return fDate;} catch (Exception e) {return null;}}}break;case 1://周一if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {mondays++;if (mondays == weekCount) {String fDateStr = year + "-" + month + "-" + i;try {Date fDate = DateUtils.parseDate(fDateStr, "yyyy-MM-dd");return fDate;} catch (Exception e) {return null;}}}break;case 2://周二if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {tuesdays++;if (tuesdays == weekCount) {String fDateStr = year + "-" + month + "-" + i;try {Date fDate = DateUtils.parseDate(fDateStr, "yyyy-MM-dd");return fDate;} catch (Exception e) {return null;}}}break;case 3://周三if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {wednesdays++;if (wednesdays == weekCount) {String fDateStr = year + "-" + month + "-" + i;try {Date fDate = DateUtils.parseDate(fDateStr, "yyyy-MM-dd");return fDate;} catch (Exception e) {return null;}}}break;case 4://周四if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {thursdays++;if (thursdays == weekCount) {String fDateStr = year + "-" + month + "-" + i;try {Date fDate = DateUtils.parseDate(fDateStr, "yyyy-MM-dd");return fDate;} catch (Exception e) {return null;}}}break;case 5://周五if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {fridays++;if (fridays == weekCount) {String fDateStr = year + "-" + month + "-" + i;try {Date fDate = DateUtils.parseDate(fDateStr, "yyyy-MM-dd");return fDate;} catch (Exception e) {return null;}}}break;case 6://周六if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {saturdays++;if (saturdays == weekCount) {String fDateStr = year + "-" + month + "-" + i;try {Date fDate = DateUtils.parseDate(fDateStr, "yyyy-MM-dd");return fDate;} catch (Exception e) {return null;}}}break;default:break;}}return null;} } //最后一周周幾的節(jié)日 @Service @Slf4j public class LastWeekFestivalFactoryImpl implements FestivalFactory {@Autowiredprivate FestivalDao festivalDao;@Autowiredprivate FestivalTimeDao festivalTimeDao;@Overridepublic boolean createFestival(FestivalDto festivalDto) {//創(chuàng)建近3年的節(jié)日LastWeekFestivalDto lastWeekDto = (LastWeekFestivalDto)festivalDto;FestivalDo festivalDo = new FestivalDo();festivalDo = DomainConvertUtils.festivalConvertDo(lastWeekDto,festivalDo);festivalDo.setMonthNum(lastWeekDto.getMonthNum());festivalDo.setWeekNum(lastWeekDto.getWeekNum());festivalDao.insertSelective(festivalDo);FestivalDo fDo = festivalDao.selectByName(lastWeekDto.getName());if (Objects.isNull(fDo)) {return false;}List<FestivalTimeDo> festivalTimeDos = Lists.newArrayList();//獲取當(dāng)前年Calendar rightNow = Calendar.getInstance();int year = rightNow.get(Calendar.YEAR);// 獲取當(dāng)前年份for (int i=0;i<50;i++) {Date result = getSundayFestival(year + i, lastWeekDto.getMonthNum() - 1, lastWeekDto.getWeekNum());if (result == null) {break;}FestivalTimeDo festivalTimeDo = new FestivalTimeDo();festivalTimeDo.setFestivalId(fDo.getId());festivalTimeDo.setStatus(1);festivalTimeDo.setTime(result);festivalTimeDos.add(festivalTimeDo);}log.info("[LastWeekFestivalFactoryImpl]festivalTimeDos:{}", JSON.toJSONString(festivalTimeDos));if (CollectionUtils.isNotEmpty(festivalTimeDos)) {festivalTimeDao.batchInsert(festivalTimeDos);}return true;}/*** 最后一周周幾的節(jié)日* @param year* @param month* @param weekNum 周幾*/private Date getSundayFestival(int year, int month, int weekNum) {//獲取當(dāng)前年的第一天String dateStr = year+"-"+month+"-"+01;Date date = null;Date result = null;try {date = DateUtils.parseDate(dateStr, "yyyy-MM-dd");} catch (Exception e) {return date;}Calendar d = Calendar.getInstance();d.setTime(date);d.set(d.MONTH, month);switch (weekNum) {case 7://周日d.set(d.DAY_OF_WEEK, d.SUNDAY);d.set(d.WEEK_OF_MONTH, d.getActualMaximum(d.WEEK_OF_MONTH));while (d.get(d.MONTH) > month || d.get(d.MONTH) < month) {d.add(d.WEEK_OF_MONTH, -1);}result = d.getTime();break;case 1://周一d.set(d.DAY_OF_WEEK, d.MONDAY);d.set(d.WEEK_OF_MONTH, d.getActualMaximum(d.WEEK_OF_MONTH));while (d.get(d.MONTH) > month || d.get(d.MONTH) < month) {d.add(d.WEEK_OF_MONTH, -1);}result = d.getTime();break;case 2://周二d.set(d.DAY_OF_WEEK, d.TUESDAY);d.set(d.WEEK_OF_MONTH, d.getActualMaximum(d.WEEK_OF_MONTH));while (d.get(d.MONTH) > month || d.get(d.MONTH) < month) {d.add(d.WEEK_OF_MONTH, -1);}result = d.getTime();break;case 3://周三d.set(d.DAY_OF_WEEK, d.WEDNESDAY);d.set(d.WEEK_OF_MONTH, d.getActualMaximum(d.WEEK_OF_MONTH));while (d.get(d.MONTH) > month || d.get(d.MONTH) < month) {d.add(d.WEEK_OF_MONTH, -1);}result = d.getTime();break;case 4://周四d.set(d.DAY_OF_WEEK, d.THURSDAY);d.set(d.WEEK_OF_MONTH, d.getActualMaximum(d.WEEK_OF_MONTH));while (d.get(d.MONTH) > month || d.get(d.MONTH) < month) {d.add(d.WEEK_OF_MONTH, -1);}result = d.getTime();break;case 5://周五d.set(d.DAY_OF_WEEK, d.FRIDAY);d.set(d.WEEK_OF_MONTH, d.getActualMaximum(d.WEEK_OF_MONTH));while (d.get(d.MONTH) > month || d.get(d.MONTH) < month) {d.add(d.WEEK_OF_MONTH, -1);}result = d.getTime();break;case 6://周六d.set(d.DAY_OF_WEEK, d.SATURDAY);d.set(d.WEEK_OF_MONTH, d.getActualMaximum(d.WEEK_OF_MONTH));while (d.get(d.MONTH) > month || d.get(d.MONTH) < month) {d.add(d.WEEK_OF_MONTH, -1);}result = d.getTime();break;default:break;}return result;}}最終效果
有問題私信。。。。。
總結(jié)
以上是生活随笔為你收集整理的Java实现节日维护的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: uva 297
- 下一篇: java美元兑换,(Java实现) 美元