本帖最后由 shujia_zaibeijing 于 2012-10-06 22:01:55 编辑

解决方案 »

  1.   

     public static int getBetweenDays(String t1, String t2)
       throws ParseException {
      DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      int betweenDays = 0;
      Date d1 = format.parse(t1);
      Date d2 = format.parse(t2);
      Calendar c1 = Calendar.getInstance();
      Calendar c2 = Calendar.getInstance();
      c1.setTime(d1);
      c2.setTime(d2);
      // 保证第二个时间一定大于第一个时间
      if (c1.after(c2)) {
       c1 = c2;
       c2.setTime(d1);
      }
      int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
      betweenDays = c2.get(Calendar.DAY_OF_YEAR)
        - c1.get(Calendar.DAY_OF_YEAR);
      for (int i = 0; i < betweenYears; i++) {
       c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
       betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
      }
      return betweenDays;
     }}
      

  2.   

    楼上的方法可以,不过,betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
    要改成 
    betweenDays += c1.getActualMaximum(Calendar.DAY_OF_YEAR);简单的方法 
        public static float getDaysBetweenDates(Date d1, Date d2)
        {
            float days = (float) (d1.getTime() - d2.getTime())
                    / (1000 * 60 * 60 * 24);
            
            return Math.abs(days);
        }还有 1903 到 1990 怎么会 是 1158 ??
    87年才1158天? 不对吧,
    请输入年份1903
    请输入月份3
    请输入日子3
    距离1990/1/1的天数:-714718526结果为啥是这个啊!天数应该是:1158