android 计算相差天数

  public static long CalculateDays(String date) {

        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");

        try {
            Date d2 = df.parse(FormattingDate(date));
            Date d1 = df.parse(FormattingDate(getDateTime()));

            long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别

            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff / days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff / days * (1000 * 60 * 60 * 24) / hours * (1000 * 60 * 60)) / (1000 * 60);

            return days;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

你可能感兴趣的:(android 计算相差天数)