commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r1056753 - /commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/time/DateUtils.java
Date Sat, 08 Jan 2011 18:37:05 GMT
Author: niallp
Date: Sat Jan  8 18:37:05 2011
New Revision: 1056753

URL: http://svn.apache.org/viewvc?rev=1056753&view=rev
Log:
Port LANG-594 to LANG 2.x Branch - DateUtils equal & compare functions up to most significant
field

Modified:
    commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/time/DateUtils.java

Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/time/DateUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/time/DateUtils.java?rev=1056753&r1=1056752&r2=1056753&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/time/DateUtils.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/time/DateUtils.java
Sat Jan  8 18:37:05 2011
@@ -1735,6 +1735,80 @@ public class DateUtils {
     }
     
     /**
+     * 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 2.6
+     */
+    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 2.6
+     */
+    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 2.6
+     */
+    public static int truncatedCompareTo(Calendar cal1, Calendar cal2, int field) {
+        Calendar truncatedCal1 = truncate(cal1, field);
+        Calendar truncatedCal2 = truncate(cal2, field);
+        return truncatedCal1.getTime().compareTo(truncatedCal2.getTime());
+    }
+
+    /**
+     * 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 2.6
+     */
+    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
      * 
      * @param unit A Calendar field which is a valid unit for a fragment



Mime
View raw message