Java Utililty Methods Date Compare

List of utility methods to do Date Compare

Description

The list of methods to do Date Compare are organized into topic(s).

Method

intcompareToMonthByDate(Date startDate, Date endDate)
compare To Month By Date
Calendar startCal = new GregorianCalendar();
Calendar endCal = new GregorianCalendar();
startCal.setTime(startDate);
endCal.setTime(endDate);
int a = compare(endCal, startCal, 2);
return a;
booleancompareTwoDate(Date beforeDate, Date afterDate)
compare Two Date
if (beforeDate == null && afterDate == null) {
    return false;
Calendar beforeCalendar = Calendar.getInstance();
beforeCalendar.setTime(beforeDate);
Calendar afterCalendar = Calendar.getInstance();
afterCalendar.setTime(afterDate);
return (afterCalendar.compareTo(beforeCalendar) > 0);
...
intcompareYear(java.util.Date date1, java.util.Date date2)
compare Year
if (date1 == null || date2 == null)
    return 0;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
int year1 = calendar.get(Calendar.YEAR);
calendar.setTime(date2);
int year2 = calendar.get(Calendar.YEAR);
return year1 - year2;
...
booleancompareYMD(Date begindate, Date enddate)
add by xie compare date only compare year month day
boolean isBefore = true;
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(begindate);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(enddate);
int startYear = startCalendar.get(Calendar.YEAR);
int startMonth = startCalendar.get(Calendar.MONTH);
int startDay = startCalendar.get(Calendar.DAY_OF_MONTH);
...
intcompDate(Date date1, Date date2)
comp Date
Calendar ca1 = Calendar.getInstance();
Calendar ca2 = Calendar.getInstance();
ca1.setTime(date1);
ca2.setTime(date2);
int distanceMin = ca1.get(Calendar.MINUTE) - ca2.get(Calendar.MINUTE);
return distanceMin;
intcompDate4(Date date1, Date date2)
comp Date
Calendar ca1 = Calendar.getInstance();
Calendar ca2 = Calendar.getInstance();
ca1.setTime(date1);
ca2.setTime(date2);
int day1 = ca1.get(Calendar.DAY_OF_MONTH);
int year1 = ca1.get(Calendar.YEAR);
int month1 = ca1.get(Calendar.MONTH);
int day2 = ca2.get(Calendar.DAY_OF_MONTH);
...
intgetCompareDate(Date startDate, Date endDate)
get Compare Date
if (startDate == null || endDate == null) {
    return 0;
long l = endDate.getTime() - startDate.getTime();
long d = l / (24 * 60 * 60 * 1000);
return (int) d;
intgetCompareDate(String strDate1, String strDate2, String pFormat)
get Compare Date
SimpleDateFormat fmt = new SimpleDateFormat(pFormat);
Date date1 = fmt.parse(strDate1);
Date date2 = fmt.parse(strDate2);
return compareDate(date1, date2);
booleangetCompareDate(String strDate1, String strDate2, String pFormat)
get Compare Date
SimpleDateFormat fmt = new SimpleDateFormat(pFormat);
Date date1 = fmt.parse(strDate1);
Date date2 = fmt.parse(strDate2);
return getCompareDate(date1, date2);
longgetCompareDateNum(String sDateValue1, String sDateValue2)
get Compare Date Num
long DAY = 24L * 60L * 60L * 1000L;
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
Date d1 = df.parse(sDateValue1);
Date d2 = df.parse(sDateValue2);
return ((d2.getTime() - d1.getTime()) / DAY);