Add Date equivalent of isSameDay method

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137972 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-10-13 21:48:00 +00:00
parent 097aa30a36
commit 4d7e4b5e78
2 changed files with 40 additions and 2 deletions

View File

@ -31,7 +31,7 @@
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a> * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @author Phil Steitz * @author Phil Steitz
* @since 2.0 * @since 2.0
* @version $Id: DateUtils.java,v 1.31 2004/10/08 00:09:01 scolebourne Exp $ * @version $Id: DateUtils.java,v 1.32 2004/10/13 21:48:00 scolebourne Exp $
*/ */
public class DateUtils { public class DateUtils {
@ -117,6 +117,31 @@ public class DateUtils {
public DateUtils() { public DateUtils() {
} }
//-----------------------------------------------------------------------
/**
* <p>Checks if two date objects are on the same day ignoring time.</p>
*
* <p>28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true.
* 28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false.
* </p>
*
* @param date1 the first date, not null
* @param date2 the second date, not null
* @return true if they represent the same day
* @throws IllegalArgumentException if either date is <code>null</code>
* @since 2.1
*/
public static boolean isSameDay(Date date1, Date date2) {
if (date1 == null || date2 == null) {
throw new IllegalArgumentException("The date must not be null");
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
return isSameDay(cal1, cal2);
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* <p>Checks if two calendar objects are on the same day ignoring time.</p> * <p>Checks if two calendar objects are on the same day ignoring time.</p>

View File

@ -153,7 +153,20 @@ public void testConstructor() {
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public void testIsSameDay() { public void testIsSameDay_Date() {
Date date1 = new GregorianCalendar(2004, 6, 9, 13, 45).getTime();
Date date2 = new GregorianCalendar(2004, 6, 9, 13, 45).getTime();
assertEquals(true, DateUtils.isSameDay(date1, date2));
date2 = new GregorianCalendar(2004, 6, 10, 13, 45).getTime();
assertEquals(false, DateUtils.isSameDay(date1, date2));
date1 = new GregorianCalendar(2004, 6, 10, 13, 45).getTime();
assertEquals(true, DateUtils.isSameDay(date1, date2));
date2 = new GregorianCalendar(2005, 6, 10, 13, 45).getTime();
assertEquals(false, DateUtils.isSameDay(date1, date2));
}
//-----------------------------------------------------------------------
public void testIsSameDay_Cal() {
GregorianCalendar cal1 = new GregorianCalendar(2004, 6, 9, 13, 45); GregorianCalendar cal1 = new GregorianCalendar(2004, 6, 9, 13, 45);
GregorianCalendar cal2 = new GregorianCalendar(2004, 6, 9, 13, 45); GregorianCalendar cal2 = new GregorianCalendar(2004, 6, 9, 13, 45);
assertEquals(true, DateUtils.isSameDay(cal1, cal2)); assertEquals(true, DateUtils.isSameDay(cal1, cal2));