根据入职日期计算工龄

public class TimerUtil {
 
	/**
	 * 计算工龄  日期格式为yyyy-MM
	 * @param t1 入职日期
	 * @param t2 当前日期
	 * @return XX年XX月
	 */
	public static String gl(String t1,String t2) {
		String[] split1 = t1.split("-");
		String[] split2 = t2.split("-");
		
		int m2 = Integer.parseInt(split2[1]);
		int m1 = Integer.parseInt(split1[1]);
		int month=0;
		int year = 0;
		if(m2 >= m1) {
			month = m2 -m1;
			year = Integer.parseInt(split2[0]) - Integer.parseInt(split1[0]);
		}else {
			month = 12-m1 +m2;
			year = Integer.parseInt(split2[0])- 1 - Integer.parseInt(split1[0]);
		}
		
		String rest="";
		if(year == 0) {
			rest=month+"月";
		}else if(month == 0) {
			rest=year+"年";
		}else {
			rest = year+"年"+month+"月";
		}
		return rest;
	}
	public static void main(String[] args) {
		String gl = TimerUtil.gl("2019-07", "2020-06");
		System.out.println(gl);
	}
}

  

原文地址:https://www.cnblogs.com/xianz666/p/14022994.html