LANG-594: Add truncatedEquals and truncatedCompareTo

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@916906 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Paul C. Benedict Jr 2010-02-27 03:29:43 +00:00
parent e49c05bdce
commit 7721acd0ef
1 changed files with 75 additions and 0 deletions

View File

@ -48,6 +48,7 @@
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @author Phil Steitz
* @author Robert Scholte
* @author Paul Benedict
* @since 2.0
* @version $Id$
*/
@ -1693,6 +1694,80 @@ private static long getFragment(Calendar calendar, int fragment, int unit) {
return result;
}
/**
* Determines if two calendars are equal up to no more than the specified
* most significant field.
*
* @param cal1 the first calendar, not <code>null</code>
* @param cal2 the second calendar, not <code>null</code>
* @param field the field from <code>Calendar</code>
* @return <code>true</code> if equal; otherwise <code>false</code>
* @throws IllegalArgumentException if any argument is <code>null</code>
* @see #truncate(Calendar, int)
* @see #truncatedEquals(Date, Date, int)
* @since 3.0
*/
public static boolean truncatedEquals(Calendar cal1, Calendar cal2, int field) {
return truncatedCompareTo(cal1, cal2, field) == 0;
}
/**
* Determines if two dates are equal up to no more than the specified
* most significant field.
*
* @param date1 the first date, not <code>null</code>
* @param date2 the second date, not <code>null</code>
* @param field the field from <code>Calendar</code>
* @return <code>true</code> if equal; otherwise <code>false</code>
* @throws IllegalArgumentException if any argument is <code>null</code>
* @see #truncate(Date, int)
* @see #truncatedEquals(Calendar, Calendar, int)
* @since 3.0
*/
public static boolean truncatedEquals(Date date1, Date date2, int field) {
return truncatedCompareTo(date1, date2, field) == 0;
}
/**
* Determines how two calendars compare up to no more than the specified
* most significant field.
*
* @param cal1 the first calendar, not <code>null</code>
* @param cal2 the second calendar, not <code>null</code>
* @param field the field from <code>Calendar</code>
* @return a negative integer, zero, or a positive integer as the first
* calendar is less than, equal to, or greater than the second.
* @throws IllegalArgumentException if any argument is <code>null</code>
* @see #truncate(Calendar, int)
* @see #truncatedCompareTo(Date, Date, int)
* @since 3.0
*/
public static int truncatedCompareTo(Calendar cal1, Calendar cal2, int field) {
Calendar truncatedCal1 = truncate(cal1, field);
Calendar truncatedCal2 = truncate(cal2, field);
return truncatedCal1.compareTo(truncatedCal2);
}
/**
* Determines how two dates compare up to no more than the specified
* most significant field.
*
* @param date1 the first date, not <code>null</code>
* @param date2 the second date, not <code>null</code>
* @param field the field from <code>Calendar</code>
* @return a negative integer, zero, or a positive integer as the first
* date is less than, equal to, or greater than the second.
* @throws IllegalArgumentException if any argument is <code>null</code>
* @see #truncate(Calendar, int)
* @see #truncatedCompareTo(Date, Date, int)
* @since 3.0
*/
public static int truncatedCompareTo(Date date1, Date date2, int field) {
Date truncatedDate1 = truncate(date1, field);
Date truncatedDate2 = truncate(date2, field);
return truncatedDate1.compareTo(truncatedDate2);
}
/**
* Returns the number of millis of a datefield, if this is a constant value
*