Java Calendar Between daysBetween(Calendar start, Calendar end)

Here you can find the source of daysBetween(Calendar start, Calendar end)

Description

days Between

License

Open Source License

Declaration

public static long daysBetween(Calendar start, Calendar end) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) Emil Crumhorn - Hexapixel.com - emil.crumhorn@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w  w w.j  ava 2  s.c  om
 *    emil.crumhorn@gmail.com - initial API and implementation
 *******************************************************************************/

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class Main {
    private static final long MILLISECONDS_IN_DAY = 24 * 60 * 60 * 1000;

    public static long daysBetween(Calendar start, Calendar end) {
        // create copies
        GregorianCalendar startDate = new GregorianCalendar(Locale.getDefault());
        GregorianCalendar endDate = new GregorianCalendar(Locale.getDefault());

        // switch calendars to pure Julian mode for correct day-between calculation, from the Java API:
        // - To obtain a pure Julian calendar, set the change date to Date(Long.MAX_VALUE).
        startDate.setGregorianChange(new Date(Long.MAX_VALUE));
        endDate.setGregorianChange(new Date(Long.MAX_VALUE));

        // set them
        startDate.setTime(start.getTime());
        endDate.setTime(end.getTime());

        // force times to be exactly the same
        startDate.set(Calendar.HOUR_OF_DAY, 12);
        endDate.set(Calendar.HOUR_OF_DAY, 12);
        startDate.set(Calendar.MINUTE, 0);
        endDate.set(Calendar.MINUTE, 0);
        startDate.set(Calendar.SECOND, 0);
        endDate.set(Calendar.SECOND, 0);
        startDate.set(Calendar.MILLISECOND, 0);
        endDate.set(Calendar.MILLISECOND, 0);

        // now we should be able to do a "safe" millisecond/day caluclation to get the number of days
        long endMilli = endDate.getTimeInMillis();
        long startMilli = startDate.getTimeInMillis();

        // calculate # of days, finally
        long diff = (endMilli - startMilli) / MILLISECONDS_IN_DAY;

        return diff;
    }

    public static long daysBetween(Date start, Date end) {
        Calendar dEnd = Calendar.getInstance(Locale.getDefault());
        Calendar dStart = Calendar.getInstance(Locale.getDefault());
        dEnd.setTime(end);
        dStart.setTime(start);
        return daysBetween(dStart, dEnd);
    }
}

Related

  1. daysBetween(Calendar early, Calendar late)
  2. daysBetween(Calendar early, Calendar late)
  3. daysBetween(Calendar fromCalendar, Calendar untilCalendar)
  4. daysBetween(Calendar fromDate, Calendar toDate)
  5. daysBetween(Calendar startTime, Calendar endTime)
  6. daysBetween(final Calendar cal1, final Calendar cal2)
  7. daysBetween(final Calendar startDate, final Calendar endDate)
  8. daysBetween(final Calendar startDate, final Calendar endDate)