考虑闰年计算年龄

时间:2013-12-15 15:41:30

标签: android

方法getDaysRemainingForNextBirthDay()对闰年不起作用,即。而不是在1988年剩余的12天,它显示377天...如何改变代码使其适用于....

public long getDaysRemainingForNextBirthDay(){

          String startDate=year+"/"+month+"/"+day;        
          SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
          Date date=null;
          try{
              date=sdf.parse(startDate);
          }
          catch (Exception e) {
            e.printStackTrace();
        }
          Calendar BDay=Calendar.getInstance();
          BDay.setTime(date);
          Calendar today=Calendar.getInstance();
          int BMonth=BDay.get(Calendar.MONTH);
          int CMonth=today.get(Calendar.MONTH);
          BDay.set(Calendar.YEAR,today.get(Calendar.YEAR));
          if (BMonth<=CMonth) {
            BDay.set(Calendar.YEAR,today.get(Calendar.YEAR)+1);
        }
          long millis=(BDay.getTimeInMillis())-(today.getTimeInMillis());
          long remainingDays=millis/(86400000);
          return (remainingDays+1);
      }

0 个答案:

没有答案