2024 计算退休年龄、日期、延长月数【建议多测试几遍】

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]));
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值