/**
* 獲取指定年月的第一天
* @param year
* @param month
* @return
*/
public static Date getFirstDayOfMonth1(int year, int month) throws ParseException {
Calendar cal = Calendar.getInstance();
//設置年份
cal.set(Calendar.YEAR, year);
//設置月份
cal.set(Calendar.MONTH, month-1);
//獲取某月最小天數
int firstDay = cal.getMinimum(Calendar.DATE);
//設置日歷中月份的最小天數
cal.set(Calendar.DAY_OF_MONTH,firstDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(sdf.format(cal.getTime()));
return date;
}
/**
* 獲取指定年月的最后一天
* @param year
* @param month
* @return
*/
public static Date getLastDayOfMonth1(int year, int month) throws ParseException {
Calendar cal = Calendar.getInstance();
//設置年份
cal.set(Calendar.YEAR, year);
//設置月份
cal.set(Calendar.MONTH, month-1);
//獲取某月最大天數
int lastDay = cal.getActualMaximum(Calendar.DATE);
//設置日歷中月份的最大天數
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(sdf.format(cal.getTime()));
return date;
}
/*時間比大小*/
public int timeCompare(String t1,String t2){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
Calendar c1=Calendar.getInstance();
Calendar c2=Calendar.getInstance();
try {
c1.setTime(formatter.parse(t1));
c2.setTime(formatter.parse(t2));
} catch (ParseException e) {
e.printStackTrace();
}
int result=c1.compareTo(c2);
return result;
}
/*時間相減獲取天數*/
public static int differentDaysByMillisecond(Date date1,Date date2)
{
// int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
long days=(date2.getTime()-date1.getTime())/(24*60*60*1000)+1;
return (int) days;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String[] time = statistics.getAttenceDate().split("-");
if (time.length!=2){
ResultUtil.error("年月格式不規范");
}
askLeaves.forEach(askLeave -> {
try {
//開始日期結束日期都在當前月
if(timeCompare(statistics.getAttenceDate(),askLeave.getStartDate())==0&&timeCompare(statistics.getAttenceDate(),askLeave.getEndDate())==0){
//獲取請假天數
int day = differentDaysByMillisecond(sdf.parse(askLeave.getStartDate()),sdf.parse(askLeave.getEndDate()));
if(hashMap.get(askLeave.getLeaveTypeCode())==null){
hashMap.put(askLeave.getLeaveTypeCode(),day);
}else{
hashMap.put(askLeave.getLeaveTypeCode(),hashMap.get(askLeave.getLeaveTypeCode())+day);
}
}
//開始月在當前月結束月不在
if(timeCompare(statistics.getAttenceDate(),askLeave.getStartDate())==0&&timeCompare(statistics.getAttenceDate(),askLeave.getEndDate())!=0) {
// //獲取指定月最后一天
int day = differentDaysByMillisecond(sdf.parse(askLeave.getStartDate()),getLastDayOfMonth1(Integer.valueOf(time[0]),Integer.valueOf(time[1])));
hashMap.put(askLeave.getLeaveTypeCode(),day);
}
//結束月在當前月開始月不在
if(timeCompare(statistics.getAttenceDate(),askLeave.getStartDate())!=0&&timeCompare(statistics.getAttenceDate(),askLeave.getEndDate())==0) {
// calendar.add(Calendar.MONTH, 0);
//獲取指定月第一天
int day = differentDaysByMillisecond(getFirstDayOfMonth1(Integer.valueOf(time[0]),Integer.valueOf(time[1])),sdf.parse(askLeave.getEndDate()));
hashMap.put(askLeave.getLeaveTypeCode(),day);
}
} catch (Exception e) {
e.printStackTrace();
}
});