目录
前言
好久没有进行文章记录了,今天完成了一个渐进式延迟法定退休年龄计算的java实现方法。亲写、亲测没毛病,记录下来以备后用。估计网上都有好多了。也不知道是否有雷同。鉴于自己写的,所以记录下来。
一、关于延迟退休的规定
从2025年1月1日起,男职工和原法定退休年龄为五十五周岁的女职工,法定退休年龄每四个月延迟一个月,分别逐步延迟至六十三周岁和五十八周岁;原法定退休年龄为五十周岁的女职工,法定退休年龄每二个月延迟一个月,逐步延迟至五十五周岁。
二、计算方法
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
/**
* 渐进式延迟法定退休年龄计算
* 2024年12月12日
*/
public class Testsaas {
/**
* 法定退休年龄
*
* @param cs_ym 出生年月
* @param sex 性别,0女性55退休,1男60岁退休,2女50岁退休
* @return
*/
public String fdTxAge(String cs_ym, int sex) throws Exception {
String strTx;
int year = Integer.parseInt(cs_ym.substring(0, 4)); //出生年
String monthStr = cs_ym.substring(4, 6); //出生月
int month = Integer.parseInt(monthStr); //出生月,int型
String txYm;//最终退休年月
String txAge;//最终退休年龄
if (sex == 0 || sex == 1 || sex == 2) {
int yTxYear = year + (sex == 2 ? 50 : sex == 0 ? 55 : 60); //得到原法定退休年
/*因为2025年1月1日起实行渐进式延迟法定退休,那么原50岁1975年1月、55岁1970年1月往后出生的女性和60岁1965年1月往后出生的男性在新规范围内*/
int s_year = (sex == 2 ? 1975 : sex == 0 ? 1970 : 1965);
if (year < s_year) {
txYm = yTxYear + monthStr; //拼接字符串,不是数字计算
strTx = "出生日期:" + cs_ym + ",退休年龄:" + (yTxYear - year) + "岁,不用延迟退休,正常退休年月:" + txYm;
} else {
/*因为2025年1月1日起实行渐进式延迟法定退休,那么原50岁1975年1月、55岁1970年1月往后出生的女性和60岁1965年1月往后出生的男性在新规范围内*/
YearMonth startYm = YearMonth.of(s_year - 1, 12);
YearMonth endCsYm = YearMonth.of(year, month); //出生年,月
long ycTxMmCount = startYm.until(endCsYm, ChronoUnit.MONTHS); //延迟退休的实际月数,until()方法得到两个年月之间的差值月数
long ycm2or4 = (long) Math.ceil((double) ycTxMmCount / (sex == 2 ? 2 : 4));//每2(原规定50岁退休的女性)或4个月延迟1个月的折算延迟月数,ceil()将double类型向上舍入到最接近的整数
//最多只能延迟36或60(原规定50岁退休的女性)个月退休
if ((sex == 0 || sex == 1) && ycm2or4 > 36) {
ycm2or4 = 36;
}
if (sex == 2 && ycm2or4 > 60) {
ycm2or4 = 60;
}
// 创建LocalDate实例
LocalDate date = LocalDate.of(yTxYear, month, 1);
// 原规定退休年月上加延迟退休月数,得到延迟后的退休年月
LocalDate futureDate = date.plusMonths(ycm2or4);
txYm = futureDate.format(DateTimeFormatter.ofPattern("yyyyMM"));//格式化得到延迟退休的年月
int tx_y = Integer.parseInt(txYm.substring(0, 4));//退休年
int tx_m = Integer.parseInt(txYm.substring(4, 6));//退休月
YearMonth endTxYm = YearMonth.of(tx_y, tx_m);//退休年月,实例化YearMonth对象
long monthsBetween = endCsYm.until(endTxYm, ChronoUnit.MONTHS);//得到相差月数
long age = Math.floorDiv(monthsBetween, 12);//得到整数位,年龄。floorDiv()方法两数相除向下取整
long age_mm = (monthsBetween - (age * 12));//得到剩余月数
txAge = age + "岁" + (age_mm == 0 ? "" : age_mm + "个月");
strTx = "出生日期:" + cs_ym + ",退休年龄:" + txAge + ",退休日期:" + txYm + ",延迟退休月数:" + ycm2or4;
}
} else {
strTx = "查询条件输入有误!。。。。。。";
}
return strTx;
}
public static void main(String[] args) {
Testsaas test = new Testsaas();
String csYm = "198412";
int year = Integer.parseInt(csYm.substring(0, 4)); //出生年
System.out.println("*****原来50岁退休年:" + (year + 50));
System.out.println("______55岁退休年:" + (year + 55));
try {
System.out.println("====延迟退休计算结果:" + test.fdTxAge(csYm, 2));
} catch (Exception e) {
e.printStackTrace();
}
}
}
整个代码全部在此,亲测 OK。直接复制运行。不足之处请指正。注意java版本-JDK。