Java Date Compare compareDates(Date date1, Date date2)

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

Description

Compares two given valid dates to return a number flag

License

Open Source License

Parameter

Parameter Description
date1 Date object
date2 Date object

Return

Number flag as per following -1 Inputs are wrong (null) 0 Dates are equal 1 First date is before the second one 2 First date is after the second one

Declaration

public static int compareDates(Date date1, Date date2) 

Method Source Code


//package com.java2s;
import java.util.*;
import java.text.*;

public class Main {
    /**/* www. j  ava2s . c  o m*/
     * Default Date format
     */
    public static final String DATE_FORMAT = "dd-MM-yyyy";
    /**
     * MONTH
     */
    public static final int MONTH = 2;
    /**
     * YEAR
     */
    public static final int YEAR = 3;

    /**
     * Compares two given valid dates to return a number flag
     *
     * @param dateStr1 Date format string ('MM/DD/YYYY HH24:MM:SS' format)
     * @param dateStr2 Date format string ('MM/DD/YYYY HH24:MM:SS' format)
     *
     * @return Number flag as per following -1 Inputs are wrong (null) 0 Dates
     * are equal 1 First date is before the second one 2 First date is after the
     * second one
     */
    public static int compareDates(String dateStr1, String dateStr2) {
        // Parse the date
        Date date1 = parseDate(dateStr1);
        Date date2 = parseDate(dateStr2);

        // Return
        return compareDates(date1, date2);
    }

    /**
     * Compares two given valid dates to return a number flag
     *
     * @param date1 Date object
     * @param date2 Date object
     *
     * @return Number flag as per following -1 Inputs are wrong (null) 0 Dates
     * are equal 1 First date is before the second one 2 First date is after the
     * second one
     */
    public static int compareDates(Date date1, Date date2) {
        // Check input
        if (date1 == null || date2 == null) {
            return -1;
        }

        int dateDifference = 0;

        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);

        // Retrieve days, months and years for both dates
        int day1 = calendar1.get(Calendar.DATE);
        int month1 = calendar1.get(Calendar.MONTH);
        int year1 = calendar1.get(Calendar.YEAR);

        int day2 = calendar2.get(Calendar.DATE);
        int month2 = calendar2.get(Calendar.MONTH);
        int year2 = calendar2.get(Calendar.YEAR);

        // Compare years
        if (year1 < year2) {
            dateDifference = 1;
        } else if (year1 > year2) {
            dateDifference = 2;
        } // Years are same so compare months
        else {
            if (month1 < month2) {
                dateDifference = 1;
            } else if (month1 > month2) {
                dateDifference = 2;
            } // Months are same so compare days
            else {
                if (day1 < day2) {
                    dateDifference = 1;
                } else if (day1 > day2) {
                    dateDifference = 2;
                } else {
                    dateDifference = 0;
                }
            }
        }

        // Return
        return dateDifference;
    }

    /**
     * Return a Date for the DATE_FORMAT format string
     *
     * @param dateStr Date format string
     *
     * @return Java Date object if the input is valid, else null
     */
    public static Date parseDate(String dateStr) {
        // Check input
        if (dateStr == null) {
            return null;
        }

        // Create the formatter object
        SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);

        // Do not parse the string if it do not adhere to the format given
        formatter.setLenient(false);

        // Parse
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(dateStr, pos);

        // Return
        return date;
    }

    /**
     * Return a String for the given java.util.Date as per DATE_FORMAT
     *
     * @param date Date object
     *
     * @return String for the given java.util.Date as per DATE_FORMAT, null in
     * case of error
     */
    public static String parseDate(Date date) {
        // Check input
        if (date == null) {
            return null;
        }

        // Create the formatter object
        SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);

        // Do not parse the string if it do not adhere to the format given
        formatter.setLenient(false);

        // Parse
        return formatter.format(date);
    }
}

Related

  1. compareDate(String startDate, String endDate, String strFormat)
  2. compareDate(String strDate1, String strDate2, String format)
  3. compareDate2(String begingDate, String endDate, String format)
  4. compareDateFormat(String strDate1, String strDate2, String format)
  5. compareDateForSecond(Date src, Date target, int second)
  6. compareDates(Date date1, Date date2)
  7. compareDates(final Date dateA, final Date dateB)
  8. compareDates(final String format, final Date date1, final Date date2)
  9. compareDateSequence(String startDate, String endDate)