繁体   English   中英

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

[英]How to calculate a full days difference between two dates considering daylight savings in java

我需要获取java中两个日期之间的整天(日期以Date类型给出)。
例如:01/01/2015/12:00:00-01/02/2015/11:59:00不是一整天,我需要考虑夏令时。
我知道jodatime lib可以做到这一点,但是我达到了65k方法的极限,我无法使用jodatime lib。
我尝试了毫秒差异方法和使用“之前”方法的while循环: Android / Java-天中的日期差异

我设法弄清楚:我使用了一些代码-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);
    }
}
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM