commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang RELEASE-NOTES.txt
Date Sat, 16 Oct 2004 17:08:43 GMT
scolebourne    2004/10/16 10:08:43

  Modified:    lang/src/test/org/apache/commons/lang/time
                        DateUtilsTest.java
               lang/src/java/org/apache/commons/lang/time DateUtils.java
               lang     RELEASE-NOTES.txt
  Log:
  Add methods to compare dates and calendars in different ways
  based on bug 26922
  
  Revision  Changes    Path
  1.19      +61 -0     jakarta-commons/lang/src/test/org/apache/commons/lang/time/DateUtilsTest.java
  
  Index: DateUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/time/DateUtilsTest.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- DateUtilsTest.java	13 Oct 2004 21:48:00 -0000	1.18
  +++ DateUtilsTest.java	16 Oct 2004 17:08:42 -0000	1.19
  @@ -163,6 +163,10 @@
           assertEquals(true, DateUtils.isSameDay(date1, date2));
           date2 = new GregorianCalendar(2005, 6, 10, 13, 45).getTime();
           assertEquals(false, DateUtils.isSameDay(date1, date2));
  +        try {
  +            DateUtils.isSameDay((Date) null, (Date) null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
       }
       
       //-----------------------------------------------------------------------
  @@ -176,6 +180,63 @@
           assertEquals(true, DateUtils.isSameDay(cal1, cal2));
           cal2.add(Calendar.YEAR, 1);
           assertEquals(false, DateUtils.isSameDay(cal1, cal2));
  +        try {
  +            DateUtils.isSameDay((Calendar) null, (Calendar) null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +    }
  +    
  +    //-----------------------------------------------------------------------
  +    public void testIsSameInstant_Date() {
  +        Date date1 = new GregorianCalendar(2004, 6, 9, 13, 45).getTime();
  +        Date date2 = new GregorianCalendar(2004, 6, 9, 13, 45).getTime();
  +        assertEquals(true, DateUtils.isSameInstant(date1, date2));
  +        date2 = new GregorianCalendar(2004, 6, 10, 13, 45).getTime();
  +        assertEquals(false, DateUtils.isSameInstant(date1, date2));
  +        date1 = new GregorianCalendar(2004, 6, 10, 13, 45).getTime();
  +        assertEquals(true, DateUtils.isSameInstant(date1, date2));
  +        date2 = new GregorianCalendar(2005, 6, 10, 13, 45).getTime();
  +        assertEquals(false, DateUtils.isSameInstant(date1, date2));
  +        try {
  +            DateUtils.isSameInstant((Date) null, (Date) null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +    }
  +    
  +    //-----------------------------------------------------------------------
  +    public void testIsSameInstant_Cal() {
  +        GregorianCalendar cal1 = new GregorianCalendar(TimeZone.getTimeZone("GMT+1"));
  +        GregorianCalendar cal2 = new GregorianCalendar(TimeZone.getTimeZone("GMT-1"));
  +        cal1.set(2004, 6, 9, 13, 45, 0);
  +        cal1.set(Calendar.MILLISECOND, 0);
  +        cal2.set(2004, 6, 9, 13, 45, 0);
  +        cal2.set(Calendar.MILLISECOND, 0);
  +        assertEquals(false, DateUtils.isSameInstant(cal1, cal2));
  +        
  +        cal2.set(2004, 6, 9, 11, 45, 0);
  +        assertEquals(true, DateUtils.isSameInstant(cal1, cal2));
  +        try {
  +            DateUtils.isSameInstant((Calendar) null, (Calendar) null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +    }
  +    
  +    //-----------------------------------------------------------------------
  +    public void testIsSameLocalTime_Cal() {
  +        GregorianCalendar cal1 = new GregorianCalendar(TimeZone.getTimeZone("GMT+1"));
  +        GregorianCalendar cal2 = new GregorianCalendar(TimeZone.getTimeZone("GMT-1"));
  +        cal1.set(2004, 6, 9, 13, 45, 0);
  +        cal1.set(Calendar.MILLISECOND, 0);
  +        cal2.set(2004, 6, 9, 13, 45, 0);
  +        cal2.set(Calendar.MILLISECOND, 0);
  +        assertEquals(true, DateUtils.isSameLocalTime(cal1, cal2));
  +        
  +        cal2.set(2004, 6, 9, 11, 45, 0);
  +        assertEquals(false, DateUtils.isSameLocalTime(cal1, cal2));
  +        try {
  +            DateUtils.isSameLocalTime((Calendar) null, (Calendar) null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
       }
       
       //-----------------------------------------------------------------------
  
  
  
  1.33      +69 -6     jakarta-commons/lang/src/java/org/apache/commons/lang/time/DateUtils.java
  
  Index: DateUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/time/DateUtils.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- DateUtils.java	13 Oct 2004 21:48:00 -0000	1.32
  +++ DateUtils.java	16 Oct 2004 17:08:42 -0000	1.33
  @@ -125,8 +125,8 @@
        * 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
  +     * @param date1  the first date, not altered, not null
  +     * @param date2  the second date, not altered, not null
        * @return true if they represent the same day
        * @throws IllegalArgumentException if either date is <code>null</code>
        * @since 2.1
  @@ -142,7 +142,6 @@
           return isSameDay(cal1, cal2);
       }
   
  -    //-----------------------------------------------------------------------
       /**
        * <p>Checks if two calendar objects are on the same day ignoring time.</p>
        *
  @@ -150,8 +149,8 @@
        * 28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false.
        * </p>
        * 
  -     * @param cal1  the first calendar, not null
  -     * @param cal2  the second calendar, not null
  +     * @param cal1  the first calendar, not altered, not null
  +     * @param cal2  the second calendar, not altered, not null
        * @return true if they represent the same day
        * @throws IllegalArgumentException if either calendar is <code>null</code>
        * @since 2.1
  @@ -163,6 +162,70 @@
           return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                   cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                   cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     * <p>Checks if two date objects represent the same instant in time.</p>
  +     *
  +     * <p>This method compares the long millisecond time of the two objects.</p>
  +     * 
  +     * @param date1  the first date, not altered, not null
  +     * @param date2  the second date, not altered, not null
  +     * @return true if they represent the same millisecond instant
  +     * @throws IllegalArgumentException if either date is <code>null</code>
  +     * @since 2.1
  +     */
  +    public static boolean isSameInstant(Date date1, Date date2) {
  +        if (date1 == null || date2 == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
  +        return (date1.getTime() == date2.getTime());
  +    }
  +
  +    /**
  +     * <p>Checks if two calendar objects represent the same instant in time.</p>
  +     *
  +     * <p>This method compares the long millisecond time of the two objects.</p>
  +     * 
  +     * @param cal1  the first calendar, not altered, not null
  +     * @param cal2  the second calendar, not altered, not null
  +     * @return true if they represent the same millisecond instant
  +     * @throws IllegalArgumentException if either date is <code>null</code>
  +     * @since 2.1
  +     */
  +    public static boolean isSameInstant(Calendar cal1, Calendar cal2) {
  +        if (cal1 == null || cal2 == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
  +        return (cal1.getTime().getTime() == cal2.getTime().getTime());
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     * <p>Checks if two calendar objects represent the same local time.</p>
  +     *
  +     * <p>This method compares the values of the fields of the two objects.
  +     * In addition, both calendars must be the same of the same type.</p>
  +     * 
  +     * @param cal1  the first calendar, not altered, not null
  +     * @param cal2  the second calendar, not altered, not null
  +     * @return true if they represent the same millisecond instant
  +     * @throws IllegalArgumentException if either date is <code>null</code>
  +     * @since 2.1
  +     */
  +    public static boolean isSameLocalTime(Calendar cal1, Calendar cal2) {
  +        if (cal1 == null || cal2 == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
  +        return (cal1.get(Calendar.MILLISECOND) == cal2.get(Calendar.MILLISECOND) &&
  +                cal1.get(Calendar.SECOND) == cal2.get(Calendar.SECOND) &&
  +                cal1.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE) &&
  +                cal1.get(Calendar.HOUR) == cal2.get(Calendar.HOUR) &&
  +                cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) &&
  +                cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
  +                cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
  +                cal1.getClass() == cal2.getClass());
       }
   
       //-----------------------------------------------------------------------
  
  
  
  1.29      +3 -3      jakarta-commons/lang/RELEASE-NOTES.txt
  
  Index: RELEASE-NOTES.txt
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/RELEASE-NOTES.txt,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- RELEASE-NOTES.txt	16 Oct 2004 16:45:59 -0000	1.28
  +++ RELEASE-NOTES.txt	16 Oct 2004 17:08:42 -0000	1.29
  @@ -74,7 +74,7 @@
   
   - NumberUtils - various string to number parsing methods added
   
  -- DateUtils - isSameDay method added to compare day ignoring time
  +- DateUtils - methods added to compare dates in various ways
   - FastDateFormat - extra formatting methods that take in a millisecond long value
                   -- additional static factory methods
   - StopWatch - new methods for split behaviour
  @@ -114,4 +114,4 @@
   30815  ArrayUtils.isEquals() throws ClassCastException when array1
   31478  Compile error with JDK 5 "enum" is a keyword
   31572  o.a.c.lang.enum.ValuedEnum: 'enum'is a keyword in JDK1.5.0
  -
  +26922  public static boolean DateUtils.equals(Date dt1, Date dt2)
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message