Java Date After isAfter(Date date1, Date date2)

Here you can find the source of isAfter(Date date1, Date date2)

Description

Checks if date1 is after date2.

License

Open Source License

Parameter

Parameter Description
date1 The first given date
date2 The second given date

Return

true if date1 is after date2; false otherwise.

Declaration

public static boolean isAfter(Date date1, Date date2) 

Method Source Code

//package com.java2s;
/*//from   w ww  .  jav a 2s  .c om
 * Utility class for a easy way to handle Date
 * Copyright (C) 2012 Martin Absmeier, IT Consulting Services
 *
 *  This program 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.
 *
 *  This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
 */

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

import java.util.Locale;

public class Main {
    /**
     * Checks if date1 is after date2. It is tested without time slice.
     * 
     * @param date1
     *            The first given date
     * @param date2
     *            The second given date
     * @return <code>true</code> if date1 is after date2; <code>false</code>
     *         otherwise.
     */
    public static boolean isAfter(Date date1, Date date2) {
        int[] dIng1 = getDateIngredients(date1);
        int day1 = dIng1[0];
        int month1 = dIng1[1];
        int year1 = dIng1[2];

        int[] dIng2 = getDateIngredients(date2);
        int day2 = dIng2[0];
        int month2 = dIng2[1];
        int year2 = dIng2[2];

        if (year1 > year2) {
            return true;
        }
        if (year1 < year2) {
            return false;
        }

        if (year1 == year2 && month1 > month2) {
            return true;
        }
        if (year1 == year2 && month1 < month2) {
            return false;
        }

        if (year1 == year2 && month1 == month2 && day1 > day2) {
            return true;
        }

        return false;
    }

    /**
     * Provides an array day, month and year.<br>
     * <code>
     * int[] ingredients = getDateIngredients(date);<br>
     * int day = res[0];<br>
     * int month = res[1];<br>
     * int year = res[2];<br>
     * <code>
     * 
     * @param date
     *            The given date.
     * @return The ingredients of date
     */
    public static int[] getDateIngredients(Date date) {
        int[] ingredients = new int[3];

        Calendar cal = createCalendar(date);
        ingredients[0] = cal.get(Calendar.DATE);
        ingredients[1] = cal.get(Calendar.MONTH);
        ingredients[2] = cal.get(Calendar.YEAR);

        return ingredients;
    }

    /**
     * Creates a calendar from given date.
     * 
     * @param date
     *            the given date
     * @return the created <code>Calendar</code>.
     */
    public static Calendar createCalendar(Date date) {
        Calendar cal = Calendar.getInstance(Locale.GERMANY);
        cal.setTimeInMillis(date.getTime());

        return cal;
    }
}

Related

  1. getMonthAfter(final Date _from, final int _mons)
  2. getNDayAfterCurrentDate(int n)
  3. getSpecifiedDayAfter(Date date, int num)
  4. getSpecifiedDayAfter4Date(Date date)
  5. isAfter(Date d1, Date d2)
  6. isAfterForDay(Date date, Date ref)
  7. isAfterHour(Date source, int hour)
  8. isAfterToday(Date dag)
  9. isDateAfter(Date date1, Date date2)