Java Calendar Different getDiffPartialYears(Date date, GregorianCalendar b)

Here you can find the source of getDiffPartialYears(Date date, GregorianCalendar b)

Description

get Diff Partial Years

License

Open Source License

Declaration

public static int getDiffPartialYears(Date date, GregorianCalendar b) 

Method Source Code

//package com.java2s;
/*/*from  w ww .  java  2  s .  com*/
 * Utilities.java
 *
 * Copyright (c) 2009 Jay Lawson <jaylawson39 at yahoo.com>. All rights reserved.
 *
 * This file is part of MekHQ.
 *
 * MekHQ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MekHQ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MekHQ.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Calendar;

import java.util.Date;
import java.util.GregorianCalendar;

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

    public static int getDiffPartialYears(Date date, GregorianCalendar b) {
        GregorianCalendar a = new GregorianCalendar();
        a.setTime(date);
        int diff = b.get(GregorianCalendar.YEAR) - a.get(GregorianCalendar.YEAR);
        if (diff == 0 && countDaysBetween(a.getTime(), b.getTime()) > 0) {
            return 1;
        }
        return diff;
    }

    /**
     * Calculates the number of days between start and end dates, taking
     * into consideration leap years, year boundaries etc.
     *
     * @param start the start date
     * @param end the end date, must be later than the start date
     * @return the number of days between the start and end dates
     */
    public static long countDaysBetween(Date start, Date end) {
        if (end.before(start)) {
            throw new IllegalArgumentException("The end date must be later than the start date");
        }

        //reset all hours mins and secs to zero on start date
        Calendar startCal = GregorianCalendar.getInstance();
        startCal.setTime(start);
        startCal.set(Calendar.HOUR_OF_DAY, 0);
        startCal.set(Calendar.MINUTE, 0);
        startCal.set(Calendar.SECOND, 0);
        long startTime = startCal.getTimeInMillis();

        //reset all hours mins and secs to zero on end date
        Calendar endCal = GregorianCalendar.getInstance();
        endCal.setTime(end);
        endCal.set(Calendar.HOUR_OF_DAY, 0);
        endCal.set(Calendar.MINUTE, 0);
        endCal.set(Calendar.SECOND, 0);
        long endTime = endCal.getTimeInMillis();

        return (endTime - startTime) / MILLISECONDS_IN_DAY;
    }
}

Related

  1. getDaysDifference(Calendar refDate, Date date)
  2. getDifference(int constant, Calendar first, Calendar second)
  3. getDifference(int field, Calendar aCalX, Calendar aCalY)
  4. getDifferenceInDays(Calendar fromDate, Calendar toDate)
  5. getDifferenceInSeconds(Calendar calendarStart, Calendar calendarEnd)
  6. getDiffTargetDate(Calendar cal, int diffDay)
  7. getMillisDiff(long lhs, long rhs, int gregorianCalendarUnits)
  8. getMonthDiff(Calendar c1, Calendar c2)
  9. getMonthsDifference(Calendar date1, Calendar date2)