Java Date Compare compareDay(Date date, Date anotherDate)

Here you can find the source of compareDay(Date date, Date anotherDate)

Description

compare Day

License

Open Source License

Declaration

public static int compareDay(Date date, Date anotherDate) 

Method Source Code

//package com.java2s;

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

public class Main {

    public static int compareDay(Date date, Date anotherDate) {
        if (null == date && null == anotherDate) {
            return 0;
        }//from w  w w  .  j a va  2  s  .  c om
        if (date == null) {
            date = new Date();
        }

        if (anotherDate == null) {
            anotherDate = new Date();
        }

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date);
        cal2.setTime(anotherDate);
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);

        int month1 = cal1.get(Calendar.MONTH) + 1;
        int month2 = cal2.get(Calendar.MONTH) + 1;

        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        if (year1 == year2 && month1 == month2 && day1 == day2) {
            return 0;
        } else {
            if (year1 > year2) {
                return 1;
            } else if (month1 > month2) {
                return 1;
            } else if (day1 > day2) {
                return 1;
            } else {
                return -1;
            }
        }

    }
}

Related

  1. compareDates(final Date dateA, final Date dateB)
  2. compareDates(final String format, final Date date1, final Date date2)
  3. compareDateSequence(String startDate, String endDate)
  4. compareDatesOnly(long date1, long date2)
  5. compareDateTimeNull(Date d1, Date d2)
  6. compareDay(Date date1, int compday)
  7. compareDays(final Date date1, final Date date2)
  8. compareFileDates(final Date date1, final Date date2)
  9. compareMonth(Date first, Date second)