根据入职日期计算工龄

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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);
    }
}

  

posted @   红尘沙漏  阅读(547)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示