packagecom.xkzhangsan.time.calendar;importjava.time.LocalDateTime;importjava.time.YearMonth;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjava.util.concurrent.ConcurrentHashMap;importcom.xkzhangsan.time.calculator.DateTimeCalculatorUtil;importcom.xkzhangsan.time.formatter.DateTimeFormatterUtil;importcom.xkzhangsan.time.utils.CollectionUtil;/*** 日历工具类
* 包括:
* 1.生成指定时间的日历方法,generateCalendar* 比如generateCalendar(int year, int
* month) 生成指定年月的日历
* 2.生成指定时间的日历,包含农历和所有节假日信息方法,generateCalendarWithHoliday*, 比如generateCalendarWithHoliday(int year, int month, Map localHolidayMap,
Map chineseHolidayMap, Map dateTypeMap)生成指定年月的日历,包含农历和所有节假日信息,可以自定义节假日和工作日等。
* @ClassName: CalendarUtil
* @Description: CalendarUtil
*@authorxkzhangsan
* @date 2020年03月18日*/
public classCalendarUtil {privateCalendarUtil() {
}/*** 生成指定年月的日历
*
*@paramyear
*@parammonth
*@return
*/
public static CalendarWrapper generateCalendar(int year, intmonth) {return generateCalendar(year, month, false, false, null, null, null);
}/*** 生成指定年月的日历, 包含农历信息
*
*@paramyear
*@parammonth
*@return
*/
public static CalendarWrapper generateCalendarWithLunar(int year, intmonth) {return generateCalendar(year, month, true, false, null, null, null);
}/*** 生成指定年月的日历,包含公历节假日信息
*
*@paramyear
*@parammonth
*@paramlocalHolidayMap
* 自定义公历节日数据,特殊节日如,"母亲节", "5-W-2-7"
* 5表示5月,W表示星期,2表示第二个星期,7表示星期的第7天,为null时,使用默认数据 LocalHolidayEnum
* 比如localHolidayMap.put("0801", "建军节");
*@paramdateTypeMap 日期类型,0休息日,1等其他为工作日,比如dateTypeMap.put("2020-08-07", 0);
*@return
*/
public static CalendarWrapper generateCalendarWithLocalHoliday(int year, intmonth,
Map localHolidayMap, MapdateTypeMap) {return generateCalendar(year, month, false, true, localHolidayMap, null, dateTypeMap);
}/*** 生成指定年月的日历,包含农历和所有节假日信息
*@paramyear
*@parammonth
*@return
*/
public static CalendarWrapper generateCalendarWithHoliday(int year, intmonth) {return generateCalendar(year, month, true, true, null, null, null);
}/*** 生成指定年月的日历,包含农历和所有节假日信息,使用自定义数据
*
*@paramyear
*@parammonth
*@paramlocalHolidayMap
* 自定义公历节日数据,特殊节日如,"母亲节", "5-W-2-7"
* 5表示5月,W表示星期,2表示第二个星期,7表示星期的第7天,为null时,使用默认数据 LocalHolidayEnum
* 比如localHolidayMap.put("0801", "建军节");
*@paramchineseHolidayMap
* 自定义农历节日信息,特殊节日如除夕 用CHUXI表示,为null时,使用默认数据 ChineseHolidayEnum
* 比如chineseHolidayMap.put("0707", "七夕情人节");
*@paramdateTypeMap 日期类型,0休息日,1等其他为工作日,比如dateTypeMap.put("2020-08-07", 0);
*@return
*/
public static CalendarWrapper generateCalendarWithHoliday(int year, int month, MaplocalHolidayMap,
Map chineseHolidayMap, MapdateTypeMap) {return generateCalendar(year, month, true, true, localHolidayMap, chineseHolidayMap, dateTypeMap);
}/*** 生成指定年月的日历,包含农历和所有节假日信息
*
*@paramyear
*@parammonth
*@paramincludeLunarDate
*@paramincludeHoliday
*@paramlocalHolidayMap
* 自定义公历节日数据,特殊节日如,"母亲节", "5-W-2-7"
* 5表示5月,W表示星期,2表示第二个星期,7表示星期的第7天,为null时,使用默认数据 LocalHolidayEnum
* 比如localHolidayMap.put("0801", "建军节");
*@paramchineseHolidayMap
* 自定义农历节日信息,特殊节日如除夕 用CHUXI表示,为null时,使用默认数据 ChineseHolidayEnum
* 比如chineseHolidayMap.put("0707", "七夕情人节");
*@paramdateTypeMap 日期类型,0休息日,1等其他为工作日,比如dateTypeMap.put("2020-08-07", 0);
*@return
*/
private static CalendarWrapper generateCalendar(int year, int month, booleanincludeLunarDate,boolean includeHoliday, Map localHolidayMap, Map chineseHolidayMap, MapdateTypeMap) {
YearMonth yearMonth=YearMonth.of(year, month);
CalendarWrapper calendarWrapper= newCalendarWrapper();
Map dayMap = new ConcurrentHashMap();
List dayList = new ArrayList();
List localDateTimeList =DateTimeCalculatorUtil.getLocalDateTimeList(YearMonth.of(year, month));if(CollectionUtil.isEmpty(localDateTimeList)) {returncalendarWrapper;
}
List dayWrapperList = new ArrayList<>();
localDateTimeList.stream().forEach(localDateTime->{
DayWrapper dayWrapper= newDayWrapper(localDateTime, includeLunarDate, includeHoliday, localHolidayMap,
chineseHolidayMap);
dayWrapperList.add(dayWrapper);
dayMap.put(DateTimeFormatterUtil.formatToDateStr(localDateTime), dayWrapper);
dayList.add(dayWrapper);
});//DateType
if(CollectionUtil.isNotEmpty(dateTypeMap) &&CollectionUtil.isNotEmpty(dayMap)){
dateTypeMap.forEach((k,v)->{if(dayMap.containsKey(k)){
dayMap.get(k).setDateType(v);
}
});
}
MonthWrapper monthWrapper= newMonthWrapper(month, dayWrapperList, yearMonth.lengthOfMonth());
List monthWrapperList = new ArrayList<>();
monthWrapperList.add(monthWrapper);
YearWrapper yearWrapper= newYearWrapper(year, monthWrapperList);
List yearWrapperList = new ArrayList<>();
yearWrapperList.add(yearWrapper);
calendarWrapper= newCalendarWrapper(yearWrapperList, dayMap, dayList);returncalendarWrapper;
}/*** 生成指定年月的日历
*
*@paramyear
*@return
*/
public static CalendarWrapper generateCalendar(intyear) {return generateCalendar(year, false, false, null, null, null);
}/*** 生成指定年月的日历, 包含农历信息
*
*@paramyear
*@return
*/
public static CalendarWrapper generateCalendarWithLunar(intyear) {return generateCalendar(year, true, false, null, null, null);
}/*** 生成指定年月的日历,包含公历节假日信息
*
*@paramyear
*@paramlocalHolidayMap
* 自定义公历节日数据,特殊节日如,"母亲节", "5-W-2-7"
* 5表示5月,W表示星期,2表示第二个星期,7表示星期的第7天,为null时,使用默认数据 LocalHolidayEnum
* 比如localHolidayMap.put("0801", "建军节");
*@paramdateTypeMap 日期类型,0休息日,1等其他为工作日,比如dateTypeMap.put("2020-08-07", 0);
*@return
*/
public static CalendarWrapper generateCalendarWithLocalHoliday(int year, Map localHolidayMap, MapdateTypeMap) {return generateCalendar(year, false, true, localHolidayMap, null, dateTypeMap);
}/*** 生成指定年月的日历,包含农历和所有节假日信息
*@paramyear
*@return
*/
public static CalendarWrapper generateCalendarWithHoliday(intyear) {return generateCalendar(year, true, true, null, null, null);
}/*** 生成指定年月的日历,包含农历和所有节假日信息,使用自定义数据
*
*@paramyear
*@paramlocalHolidayMap
* 自定义公历节日数据,特殊节日如,"母亲节", "5-W-2-7"
* 5表示5月,W表示星期,2表示第二个星期,7表示星期的第7天,为null时,使用默认数据 LocalHolidayEnum
* 比如localHolidayMap.put("0801", "建军节");
*@paramchineseHolidayMap
* 自定义农历节日信息,特殊节日如除夕 用CHUXI表示,为null时,使用默认数据 ChineseHolidayEnum
* 比如chineseHolidayMap.put("0707", "七夕情人节");
*@paramdateTypeMap 日期类型,0休息日,1等其他为工作日,比如dateTypeMap.put("2020-08-07", 0);
*@return
*/
public static CalendarWrapper generateCalendarWithHoliday(int year, MaplocalHolidayMap,
Map chineseHolidayMap, MapdateTypeMap) {return generateCalendar(year, true, true, localHolidayMap, chineseHolidayMap, dateTypeMap);
}/*** 生成指定年月的日历,包含农历和所有节假日信息
*
*@paramyear
*@paramincludeLunarDate
*@paramincludeHoliday
*@paramlocalHolidayMap
* 自定义公历节日数据,特殊节日如,"母亲节", "5-W-2-7"
* 5表示5月,W表示星期,2表示第二个星期,7表示星期的第7天,为null时,使用默认数据 LocalHolidayEnum
* 比如localHolidayMap.put("0801", "建军节");
*@paramchineseHolidayMap
* 自定义农历节日信息,特殊节日如除夕 用CHUXI表示,为null时,使用默认数据 ChineseHolidayEnum
* 比如chineseHolidayMap.put("0707", "七夕情人节");
*@paramdateTypeMap 日期类型,0休息日,1等其他为工作日,比如dateTypeMap.put("2020-08-07", 0);
*@return
*/
private static CalendarWrapper generateCalendar(int year, boolean includeLunarDate, booleanincludeHoliday,
Map localHolidayMap, Map chineseHolidayMap, MapdateTypeMap) {
CalendarWrapper calendarWrapper= newCalendarWrapper();
Map dayMap = new ConcurrentHashMap();
List dayList = new ArrayList();
List monthWrapperList = new ArrayList<>();for (int i = 1; i <= 12; i++) {
YearMonth yearMonth=YearMonth.of(year, i);
List localDateTimeList =DateTimeCalculatorUtil.getLocalDateTimeList(YearMonth.of(year, i));if(CollectionUtil.isEmpty(localDateTimeList)) {continue;
}
List dayWrapperList = new ArrayList<>();
localDateTimeList.stream().forEach(localDateTime->{
DayWrapper dayWrapper= newDayWrapper(localDateTime, includeLunarDate, includeHoliday, localHolidayMap,
chineseHolidayMap);
dayWrapperList.add(dayWrapper);
dayMap.put(DateTimeFormatterUtil.formatToDateStr(localDateTime), dayWrapper);
dayList.add(dayWrapper);
});
MonthWrapper monthWrapper= newMonthWrapper(i, dayWrapperList, yearMonth.lengthOfMonth());
monthWrapperList.add(monthWrapper);
}//DateType
if(CollectionUtil.isNotEmpty(dateTypeMap) &&CollectionUtil.isNotEmpty(dayMap)){
dateTypeMap.forEach((k,v)->{if(dayMap.containsKey(k)){
dayMap.get(k).setDateType(v);
}
});
}
List yearWrapperList = new ArrayList<>();
YearWrapper yearWrapper= newYearWrapper(year, monthWrapperList);
yearWrapperList.add(yearWrapper);
calendarWrapper= newCalendarWrapper(yearWrapperList, dayMap, dayList);returncalendarWrapper;
}
}