接口 (計算開始日, 計算終了日)返回:相差月数。要求:(例)計算開始日=2007/04/10
  ・計算終了日=2007/04/20 → 0个月
  ・計算終了日=2007/05/09 → 0个月
  ・計算終了日=2007/05/10 → 1个月
  ・計算終了日=2007/05/11 → 1个月各位帮个忙。

解决方案 »

  1.   

    自己写的,赫赫,感觉涌carlander解决比较痛快点。
    但是不太会写。    private static int getMonthBetweenTwoDates(String begin, String end) {
            int beginYear = Integer.parseInt(begin.substring(0, 4));
            int beginMonth = Integer.parseInt(begin.substring(4, 6));
            int beginDay = Integer.parseInt(begin.substring(6));
            int endYear = Integer.parseInt(end.substring(0, 4));
            int endMonth = Integer.parseInt(end.substring(4, 6));
            int endDay = Integer.parseInt(end.substring(6));
            int amount = (endYear - beginYear) * 12 + (endMonth - beginMonth);
            if (amount != 0 && endDay < beginDay) {
                amount = amount - 1;
            }
            return amount;
        }
      

  2.   

    kevinliuu(@。@) ( ) 信誉:107    Blog   加为好友  2007-04-13 10:50:30  得分: 0  
     
     
       你真懒啊
      
     
      

  3.   

    public static int getMonthBetweenTwoDates(String begin, String end, String format) throws ParseException
        {
            SimpleDateFormat sf = new SimpleDateFormat(format);
            Date d1 = sf.parse(begin);
            Date d2 = sf.parse(end);
            return (int)(d2.getTime()-d1.getTime())/1000/3600/24/30;
        }