Java Date Difference diffDay(Date start, Date end)

Here you can find the source of diffDay(Date start, Date end)

Description

diff Day

License

Apache License

Declaration

public static long diffDay(Date start, Date end) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static long diffDay(Date start, Date end) {
        return offset(start, end, Calendar.DAY_OF_YEAR);
    }/*from  w ww.  ja  va2s  .  co  m*/

    private static long offset(Date start, Date end, int offsetCalendarField) {

        boolean bool = start.before(end);
        long rtn = 0;
        Calendar s = Calendar.getInstance();
        Calendar e = Calendar.getInstance();

        s.setTime(bool ? start : end);
        e.setTime(bool ? end : start);

        rtn -= s.get(offsetCalendarField);
        rtn += e.get(offsetCalendarField);

        while (s.get(Calendar.YEAR) < e.get(Calendar.YEAR)) {
            rtn += s.getActualMaximum(offsetCalendarField);
            s.add(Calendar.YEAR, 1);
        }

        return bool ? rtn : -rtn;
    }

    public static Date add(Date date, int n, int calendarField) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, n);
        return c.getTime();
    }
}

Related

  1. diffDateD(Date sd, Date ed)
  2. diffDateM(Date sd, Date ed)
  3. diffDates(long starttime, long endtime, int type)
  4. diffDateSec(java.util.Date date, java.util.Date date1)
  5. diffDay(Date date1, Date date2)
  6. diffDay(Date startDate, Date endDate)
  7. differDays(Date src, Date target)
  8. difference(Date date1, Date date2)
  9. differenceDay(Date toDate, Date fromDate)