}
private Map
Map
orderStrMap.put("01-31", "一月的月末");
orderStrMap.put("02-28", "平年二月的月末");
orderStrMap.put("02-29", "润年二月的月末");
orderStrMap.put("03-31", "三月的月末");
orderStrMap.put("04-30", "四月的月末");
orderStrMap.put("05-31", "五月的月末");
orderStrMap.put("06-30", "六月的月末");
orderStrMap.put("07-31", "七月的月末");
orderStrMap.put("08-31", "八月的月末");
orderStrMap.put("09-30", "九月的月末");
orderStrMap.put("10-31", "十月的月末");
orderStrMap.put("11-30", "十一月的月末");
orderStrMap.put("12-31", "十二月的月末");
return orderStrMap;
}
最后的代码:
//计算出每个员工的总年假
public float getAnnualLeaveTotalDay(User user) {
float annualLeaveDay = 0f;
//现在时间的年月日
Calendar currentCal = Calendar.getInstance();
int currentYear = currentCal.get(Calendar.YEAR);
int currentMonth = currentCal.get(Calendar.MONTH) + 1;
int currentDay = currentCal.get(Calendar.DATE);
//入职日期
Date entryDate = user.getEntryDate();
if (entryDate == null) {
return annualLeaveDay;
}
long diffTime = CoreDateUtils.diffDays(currentCal.getTime(), entryDate);
if (diffTime < 0) {
return annualLeaveDay;
}
//入职时间的年月日
Calendar entryCal = Calendar.getInstance();
//获取入职时间的月和日 用来判断是否是月末
String entryDateStr = CoreDateUtils.formatDateTime(entryDate);
String entryStr = entryDateStr.substring(5, 10);
entryCal.setTime(entryDate);
int entryYear = entryCal.get(Calendar.YEAR);
int entryMonth = entryCal.get(Calendar.MONTH) + 1;
int entryDay = entryCal.get(Calendar.DATE);
//获取现在时间的月和日 用来判断是否是月末
String currentDateStr = CoreDateUtils.formatDateTime(currentCal.getTime());
String currentStr = currentDateStr.substring(5, 10);
float num = 0f;
int level = userExtendService.getPositionLevel(user.getId());
if (level == PositionLevel.STAFF.getValue()) {
num = 0.5f;
} else {
num = 1f;
}
//获取年差
int yearDiff = currentYear - entryYear;
Map
//计算年假
if (currentYear == entryYear) { //入职时间和当前时间是同一年份
if (tempMap.get(entryStr) != null) { //月末入职的员工
if (tempMap.get(currentStr) != null) { //当前时间是月末的情况 2014-05-31 2014-07-31
annualLeaveDay = num * (currentMonth - entryMonth);
} else { //当前时间不是月末的情况 2014-05-31 2014-07-30 2014-02-28 2014-07-30
annualLeaveDay = num * (currentMonth - entryMonth - 1);
}
} else { //非月末入职的员工 2014-05-16 2014-07-13 2014-02-28 2014-07-30
if (currentDay < entryDay) { //2014-05-16 2014-07-13 2014-02-28 2014-07-30
annualLeaveDay = num * (currentMonth - entryMonth - 1);
} else { //2014-05-16 2014-06-18 //2014-05-16 2014-06-16
annualLeaveDay = num * (currentMonth - entryMonth);
}
}
} else { //入职时间和当前时间是不同的年份
if (currentMonth == entryMonth) { //当前月等于于入职的月份 //2013-05-16 2014-05-16 2013-05-16 2014-05-18
int entryYearNum = 0;
if (tempMap.get(entryStr) != null) { //月末入职的员工
if (tempMap.get(currentStr) != null) { //当前时间是月末的情况 2013-03-31 2014-02-28
entryYearNum = yearDiff;
} else { //当前时间不是月末的情况 2013-03-31 2014-03-27
entryYearNum = yearDiff - 1;
}
} else { //非月末入职的
if (currentDay < entryDay) {
entryYearNum = yearDiff - 1;
} else {
entryYearNum = yearDiff;
}
}
float calAnnualLeave = entryYearNum * 12 * num;
for (int i = 0; i <= entryYearNum; i++) {
if (i > 4) { //入职超过四年的年假还是多加四天,不是随着入职年数网上加 假如入职五年,年假除了每月半天外,够五年了不另加五天,而是加四天
i = 4;
}
calAnnualLeave += i;
}
if (currentDay < entryDay) {
annualLeaveDay = calAnnualLeave + 11 * num;
} else {
annualLeaveDay = calAnnualLeave;
}
} else if (currentMonth < entryMonth) { //当前月小于入职的月份 //2013-05-16 2014-03-18 //2015-02-28 2016-02-29
int entryYearNum = yearDiff - 1;
float calAnnualLeave = entryYearNum * 12 * num;
for (int i = 0; i <= entryYearNum; i++) {
if (i > 4) {
i = 4;
}
calAnnualLeave += i;
}
if (tempMap.get(entryStr) != null) { //月末入职的员工
if (tempMap.get(currentStr) != null) { //当前时间是月末的情况 2013-03-31 2014-02-28
annualLeaveDay = num * ((currentMonth + 12) - entryMonth) + calAnnualLeave;
} else { //当前时间不是月末的情况 2013-03-31 2014-02-27
annualLeaveDay = num * ((currentMonth + 12) - entryMonth - 1) + calAnnualLeave;
}
} else { //非月末入职的员工
if (currentDay < entryDay) { //当前日小于入职日 //2013-05-19 2014-03-16
annualLeaveDay = num * ((currentMonth + 12) - entryMonth - 1) + calAnnualLeave;
} else { //当前日大于等于入职日2013-05-16 2014-03-18
annualLeaveDay = num * ((currentMonth + 12) - entryMonth) + calAnnualLeave;
}
}
} else { //当前月大于等于入职的月份 //2013-01-19 2014-05-16
int entryYearNum = yearDiff;
float calAnnualLeave = entryYearNum * 12 * num;
for (int i = 0; i <= entryYearNum; i++) {
if (i > 4) {
i = 4;
}
calAnnualLeave += i;
}
if (tempMap.get(entryStr) != null) { //月末入职的员工
if (tempMap.get(currentStr) != null) { //当前时间是月末的情况 2013-03-31 2014-05-31
annualLeaveDay = num * (currentMonth - entryMonth) + calAnnualLeave;
} else { //当前时间不是月末的情况 2013-03-31 2014-05-21
annualLeaveDay = num * (currentMonth - entryMonth - 1) + calAnnualLeave;
}
} else { //非月末入职的员工
if (currentDay < entryDay) { //当前日小于入职日 //2013-05-19 2014-08-16
annualLeaveDay = num * (currentMonth - entryMonth - 1) + calAnnualLeave;
} else { //当前日大于等于入职日2013-05-16 2014-08-18
annualLeaveDay = num * (currentMonth - entryMonth) + calAnnualLeave;
}
}
}
}
return annualLeaveDay;
}