考虑到Java中的夏令时,如何计算两个日期之间的整天差异

时间:2015-09-24 20:12:38

标签: java android date calendar jodatime

我需要在java中的两个日期之间获取完整的日期(日期以日期类型给出)。
例如: 01/01/2015/12:00:00 - 01/02/2015/11:59:00不是一整天 我需要考虑夏令时 我知道jodatime lib这样做但是我达到了65k方法限制而且我不能使用jodatime lib。
我尝试了毫秒diff方式和使用“before”方法的while循环: Android/Java - Date Difference in days

2 个答案:

答案 0 :(得分:2)

我设法弄清楚: 我使用了一些代码 - https://stackoverflow.com/a/28865648/3873513 并添加了我的一些:

     public static int calcDaysDiff(Date day1, Date day2) {
    Date d1 = new Date(day1.getTime());
    Date d2 = new Date(day2.getTime());
    Calendar date1 = Calendar.getInstance();
    date1.setTime(d1);
    Calendar date2 = Calendar.getInstance();
    date2.setTime(d2);
    //checks if the start date is later then the end date - gives 0 if it is
    if (date1.get(Calendar.YEAR) >= date2.get(Calendar.YEAR)) {
        if (date1.get(Calendar.DAY_OF_YEAR) >= date2.get(Calendar.DAY_OF_YEAR)) {
            return 0;
        }
    }
    //checks if there is a daylight saving change between the two dates

    int offset = calcOffset(d1, d2);

    if (date1.get(Calendar.YEAR) > date2.get(Calendar.YEAR)) {
        //swap them
        Calendar temp = date1;
        date1 = date2;
        date2 = temp;
    }

    return calcDaysDiffAux(date1, date2) + checkFullDay(date1, date2, offset);
}

// check if there is a 24 hour diff between the 2 dates including the daylight saving offset
public static int checkFullDay(Calendar day1, Calendar day2, int offset) {
    if (day1.get(Calendar.HOUR_OF_DAY) <= day2.get(Calendar.HOUR_OF_DAY) + offset) {
        return 0;
    }
    return -1;
}

// find the number of days between the 2 dates. check only the dates and not the hours
public static int calcDaysDiffAux(final Calendar day1, final Calendar day2) {
    Calendar dayOne = (Calendar) day1.clone(),
            dayTwo = (Calendar) day2.clone();

    if (dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR)) {
        return Math.abs(dayOne.get(Calendar.DAY_OF_YEAR) - dayTwo.get(Calendar.DAY_OF_YEAR));
    } else {

        int extraDays = 0;

        while (dayTwo.get(Calendar.YEAR) > dayOne.get(Calendar.YEAR)) {
            dayTwo.add(Calendar.YEAR, -1);
            // getActualMaximum() important for leap years
            extraDays += dayTwo.getActualMaximum(Calendar.DAY_OF_YEAR);
        }

        return extraDays - day1.get(Calendar.DAY_OF_YEAR) + day2.get(Calendar.DAY_OF_YEAR);
    }
}

答案 1 :(得分:0)

public class DateDiff {
  public static void main(String[] av) {
     SimpleDateFormat myFormat = new SimpleDateFormat("MM/dd/yyyy/HH:mm:ss");

     String inputString1 = "01/01/2015/12:00:00";
     String inputString2 = "01/02/2015/11:59:00";

     try {
         Date date1 = myFormat.parse(inputString1); 
         Date date2 = myFormat.parse(inputString2);

         long diff = date2.getTime() - date1.getTime(); // Calculate the different
         int days = (int) (diff / (1000*60*60*24)); // This convert milliseconds to days

         System.out.println ("Days differ: " + days);

     } catch (Exception e) {
         e.printStackTrace();
         }  
  }
}

以下代码将计算给出的两个日期,结果打印为:

Days differ: 0