Java Date Compare compareDates(final Date dateA, final Date dateB)

Here you can find the source of compareDates(final Date dateA, final Date dateB)

Description

Compares two dates ignoring time: just year, month, day.

License

Open Source License

Return

< 0 : if dateA < dateB == 0 : if dateA == dateB > 0 : if dateA > dateB

Declaration

public static int compareDates(final Date dateA, final Date dateB) 

Method Source Code

//package com.java2s;
/**/*w  w  w.j  ava  2  s .  c  o m*/
 *
 * Licensed under the GNU General Public License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.gnu.org/licenses/gpl.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * 
 * @author Vadim Kisen
 *
 * copyright 2010 by uTest 
 */

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

public class Main {
    public static final int HOUR = Calendar.HOUR;
    public static final int MINUTE = Calendar.MINUTE;
    public static final int SECOND = Calendar.SECOND;

    /**
     * Compares two dates ignoring time: just year, month, day.
     * 
     * @return < 0 : if dateA < dateB == 0 : if dateA == dateB > 0 : if dateA >
     *         dateB
     */
    public static int compareDates(final Date dateA, final Date dateB) {

        final Calendar calendarA = GregorianCalendar.getInstance();

        calendarA.setTime(dateA);
        calendarA.set(Calendar.MILLISECOND, 0);
        calendarA.set(Calendar.SECOND, 0);
        calendarA.set(Calendar.MINUTE, 0);
        calendarA.set(Calendar.HOUR_OF_DAY, 0);

        final Calendar calendarB = GregorianCalendar.getInstance();

        calendarB.setTime(dateB);
        calendarB.set(Calendar.MILLISECOND, 0);
        calendarB.set(Calendar.SECOND, 0);
        calendarB.set(Calendar.MINUTE, 0);
        calendarB.set(Calendar.HOUR_OF_DAY, 0);

        return calendarA.getTime().compareTo(calendarB.getTime());
    }

    /**
     * Given a Date as parameter, it sets it to the specified time and returns a
     * copy.
     * 
     * @param date
     *            Original date
     * @param hours
     *            Hours to set to the date
     * @param minutes
     *            Minutes to set to the date
     * @param seconds
     *            Seconds to set to the date
     * 
     * @return A new Date with the given time.
     */
    public static Date setTime(final Date date, final int hours, final int minutes, final int seconds) {
        final Calendar aux = Calendar.getInstance();
        aux.setTime(date);
        aux.set(Calendar.MILLISECOND, 0);
        aux.set(Calendar.HOUR, hours);
        aux.set(Calendar.MINUTE, minutes);
        aux.set(Calendar.SECOND, seconds);
        return aux.getTime();
    }
}

Related

  1. compareDate2(String begingDate, String endDate, String format)
  2. compareDateFormat(String strDate1, String strDate2, String format)
  3. compareDateForSecond(Date src, Date target, int second)
  4. compareDates(Date date1, Date date2)
  5. compareDates(Date date1, Date date2)
  6. compareDates(final String format, final Date date1, final Date date2)
  7. compareDateSequence(String startDate, String endDate)
  8. compareDatesOnly(long date1, long date2)
  9. compareDateTimeNull(Date d1, Date d2)