java截取日期范围并计算相差月数

前两天,媳妇单位让整理excel的某一个单元格内两个日期范围的相差月数,本人对excel操作不是很熟练,便写了个小程序计算了一下,原始需求如下:

 

计算投资期限的范围,并得到期限范围的相差月数

思路1:得到单元格内的两个日期范围,分别截取年月日,并比较年、月、日

    public int getMonth(int row) {
        
        int result = 0;
        int coll = 14;
        
        String st = CommonF.getExcelBy07(path, index, row, coll);
        //System.out.println(st);
        String s[] = st.split("-");
        String s1 = s[0];
        String s2 = s[1];

        //System.out.println(s1);
        //System.out.println(s2);
        
        String date1[] = s1.split("\\.");
        String date2[] = s2.split("\\.");
        
        int year1 = Integer.parseInt(date1[0]);
        int month1 = Integer.parseInt(date1[1]);
        int day1 = Integer.parseInt(date1[2]);
        
        int year2 = Integer.parseInt(date2[0]);
        int month2 = Integer.parseInt(date2[1]);
        int day2 = Integer.parseInt(date2[2]);
        
        if(year1==year2) {
            if(month1==month2) {
                if(day1==day2) {
                    result = 0; 
                }
            }else if(month1<month2) {
                if(day1==1) {
                    result = month2 - month1 + 1;
                }else {
                    result = month2 - month1;
                }
            }else if(month1>month2) {
                result = month1 - month2;
            }
        }else if(year1<year2) {
            if(month1==month2) {
                if(day1==1) {
                    result = (year2 - year1)*12 + 1;
                }else {
                    result = (year2 - year1)*12;
                }
            }else if(month1<month2) {
                if(day1==1) {
                    result = (year2 - year1)*12 + (month2 - month1)+1;
                }else {
                    result = (year2 - year1)*12 + (month2 - month1);
                }
            }else if(month1>month2) {
                if(day1==1) {
                    result = (year2 - year1)*12 - (month1 - month2)+1;
                }else {
                    result = (year2 - year1)*12 - (month1 - month2);
                }
            }
        }else if(year1>year2){
            result = -2;//-2代表两个年数输入有误
        }else {
            result = -1;//-1代表日期输入有误
        }
        System.out.println("result= "+result);
        return result;
        
    }

思路2:将日期范围转换为calendar,得到年月日并做比较

     int result = 0;
        int coll = 14;
        
        String st = CommonF.getExcel(path, index, 4, coll);
        //System.out.println(st);
        String s[] = st.split("-");
        String s1 = s[0];
        String s2 = s[1];

        //System.out.println(s1);
        //System.out.println(s2);
        
        String date1[] = s1.split("\\.");
        String date2[] = s2.split("\\.");
        
        DateFormat df = new SimpleDateFormat("yyyy.MM.dd");
        try {
            Date d1 = df.parse(s[0]);
            Date d2 = df.parse(s[1]);
            
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(d1);
            c2.setTime(d2);
            System.out.println(c1.get(Calendar.YEAR));

 

posted on 2015-10-10 12:08  乔叶叶  阅读(1652)  评论(0编辑  收藏  举报

导航