public static void main(String[] args) {
calculateRetirement( "1975-12-25", "女", 50);
calculateRetirement( "1975-12-25", "女", 55);
calculateRetirement( "1975-12-25", "男", 50);
}
public static void calculateRetirement(String birthDateStr, String gender, int originalRetirementAge) {
// 延迟计算
int delayMonths = 0;
//日期格式化
Date birthday = strToDate(birthDateStr,"yyyy-MM-dd");
//2025年1月份开始执行
Integer month = getMonthInterval(dateToStr(birthday, "yyyy-MM"),"2025-01");
Integer originMonth=0;
if ("男".equals(gender)) {
originMonth = 60*12;
Integer different = originMonth-month;
// 每4个月延迟1个月
delayMonths = (different / 4) + (different % 4 == 0 ? 0 : 1);
}else if(("女".equals(gender) && originalRetirementAge == 55)) {
// 原55岁退休的女性
originMonth = 55*12;
Integer different = originMonth-month;
// 每4个月延迟1个月
delayMonths = (different / 4) + (different % 4 == 0 ? 0 : 1);
}else if ("女".equals(gender) && originalRetirementAge == 50) {
// 原50岁退休的女性
originMonth = 50*12;
Integer different = originMonth-month;
// 每2个月延迟1个月
delayMonths = (different / 2) + (different % 2 == 0 ? 0 : 1);
}
if(delayMonths>36) {
//最多延长3年
delayMonths = 36;
}
if(delayMonths<0) {
delayMonths=0;
}
Date retirementDate = DateUtil.plusMonth(birthday, originMonth+delayMonths);
Integer retirementAge = (originMonth+delayMonths)/12;
Integer retirementMonth = (originMonth+delayMonths)%12;
String str = StrUtil.format("退休日期:{},退休年龄:{},延迟退休月份:{}", DateUtil.dateToStr(retirementDate,"yyyy-MM-dd"),
retirementAge +"岁"+retirementMonth+"月", delayMonths);
System.out.println(str);
}
//字符串转日期
public static Date strToDate(String strDate,String pattern){
if(StringUtils.isEmpty(strDate)){
return null;
}
try {
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.parse(strDate);
} catch (ParseException pe) {
return null;
}
}
//日期转字符串 ,可自定义 pattern
public static String dateToStr(Date aDate,String pattern) {
if(aDate == null){
return StringUtils.EMPTY;
}
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(aDate);
}
/**
* 获取两个月份间隔的月数
* @param begin_month is not null
* @param end_month is not null
* @return 两个月份间隔的月数
* 2013-01, 2013-02 = 1
* 2013-01, 2013-01 = 0
*
*/
public static int getMonthInterval(String begin_month,String end_month){
String[] sDateArr = new String[2];
sDateArr[0] = StringUtils.left(begin_month, 4);
sDateArr[1] = StringUtils.right(begin_month, 2);
String[] eDateArr = new String[2];
eDateArr[0] = StringUtils.left(end_month, 4);
eDateArr[1] = StringUtils.right(end_month, 2);
return (Integer.parseInt(eDateArr[0])-Integer.parseInt(sDateArr[0]))*12+(Integer.parseInt(eDateArr[1])-Integer.parseInt(sDateArr[1]));
}
08-04
2333
