Author: bayard
Date: Wed Jan  9 20:41:22 2008
New Revision: 610668

URL: http://svn.apache.org/viewvc?rev=610668&view=rev
Log:
Applying the Date.setXxx replacement methods that use Calendar as supplied by 
Travis Meisenheimer in LANG-383

Modified:
    
commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateUtils.java
    
commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsTest.java

Modified: 
commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateUtils.java?rev=610668&r1=610667&r2=610668&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateUtils.java 
(original)
+++ 
commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateUtils.java 
Wed Jan  9 20:41:22 2008
@@ -403,7 +403,129 @@
         c.add(calendarField, amount);
         return c.getTime();
     }
+    
+    //-----------------------------------------------------------------------
+    /**
+     * Sets the years field to a date returning a new object.
+     * The original date object is unchanged.
+     *
+     * @param date  the date, not null
+     * @param amount the amount to set
+     * @return a new Date object set with the specified value
+     * @throws IllegalArgumentException if the date is null
+     */
+    public static Date setYears(Date date, int amount) {
+        return set(date, Calendar.YEAR, amount);
+    }
 
+    //-----------------------------------------------------------------------
+    /**
+     * Sets the months field to a date returning a new object.
+     * The original date object is unchanged.
+     *
+     * @param date  the date, not null
+     * @param amount the amount to set
+     * @return a new Date object set with the specified value
+     * @throws IllegalArgumentException if the date is null
+     */
+    public static Date setMonths(Date date, int amount) {
+        return set(date, Calendar.MONTH, amount);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Sets the day of month field to a date returning a new object.
+     * The original date object is unchanged.
+     *
+     * @param date  the date, not null
+     * @param amount the amount to set
+     * @return a new Date object set with the specified value
+     * @throws IllegalArgumentException if the date is null
+     */
+    public static Date setDays(Date date, int amount) {
+        return set(date, Calendar.DAY_OF_MONTH, amount);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Sets the hours field to a date returning a new object.  Hours range 
+     * from  0-23.
+     * The original date object is unchanged.
+     *
+     * @param date  the date, not null
+     * @param amount the amount to set
+     * @return a new Date object set with the specified value
+     * @throws IllegalArgumentException if the date is null
+     */
+    public static Date setHours(Date date, int amount) {
+        return set(date, Calendar.HOUR_OF_DAY, amount);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Sets the minute field to a date returning a new object.
+     * The original date object is unchanged.
+     *
+     * @param date  the date, not null
+     * @param amount the amount to set
+     * @return a new Date object set with the specified value
+     * @throws IllegalArgumentException if the date is null
+     */
+    public static Date setMinutes(Date date, int amount) {
+        return set(date, Calendar.MINUTE, amount);
+    }
+    
+    //-----------------------------------------------------------------------
+    /**
+     * Sets the seconds field to a date returning a new object.
+     * The original date object is unchanged.
+     *
+     * @param date  the date, not null
+     * @param amount the amount to set
+     * @return a new Date object set with the specified value
+     * @throws IllegalArgumentException if the date is null
+     */
+    public static Date setSeconds(Date date, int amount) {
+        return set(date, Calendar.SECOND, amount);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Sets the miliseconds field to a date returning a new object.
+     * The original date object is unchanged.
+     *
+     * @param date  the date, not null
+     * @param amount the amount to set
+     * @return a new Date object set with the specified value
+     * @throws IllegalArgumentException if the date is null
+     */
+    public static Date setMilliseconds(Date date, int amount) {
+        return set(date, Calendar.MILLISECOND, amount);
+    } 
+    
+    //-----------------------------------------------------------------------
+    /**
+     * Sets the specified field to a date returning a new object.  
+     * This does not use a lenient calendar.
+     * The original date object is unchanged.
+     *
+     * @param date  the date, not null
+     * @param amount the amount to set
+     * @return a new Date object set with the specified value
+     * @throws IllegalArgumentException if the date is null
+     */
+    private static Date set(Date date, int calendarField, int amount) {
+        if (date == null) {
+            throw new IllegalArgumentException("The date must not be null");
+        }
+        // getInstance() returns a new object, so this method is thread safe.
+        Calendar c = Calendar.getInstance();
+        c.setLenient(false);
+        c.setTime(date);
+        c.set(calendarField, amount);
+        return c.getTime();
+    }   
+    
     //-----------------------------------------------------------------------
     /**
      * <p>Round this date, leaving the field specified as the most

Modified: 
commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsTest.java?rev=610668&r1=610667&r2=610668&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsTest.java
 (original)
+++ 
commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsTest.java
 Wed Jan  9 20:41:22 2008
@@ -455,6 +455,151 @@
         assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
         assertDate(result, 1999, 6, 5, 4, 3, 2, 1);
     }
+    
+       // 
-----------------------------------------------------------------------
+       public void testSetYears() throws Exception {
+               Date base = new Date(MILLIS_TEST);
+               Date result = DateUtils.setYears(base, 2000);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 6, 5, 4, 3, 2, 1);
+
+               result = DateUtils.setYears(base, 2008);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2008, 6, 5, 4, 3, 2, 1);
+
+               result = DateUtils.setYears(base, 2005);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2005, 6, 5, 4, 3, 2, 1);
+       }
+
+       // 
-----------------------------------------------------------------------
+       public void testSetMonths() throws Exception {
+               Date base = new Date(MILLIS_TEST);
+               Date result = DateUtils.setMonths(base, 5);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 5, 5, 4, 3, 2, 1);
+
+               result = DateUtils.setMonths(base, 1);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 1, 5, 4, 3, 2, 1);
+
+               try {
+                       result = DateUtils.setMonths(base, 12);
+                       fail("DateUtils.setMonths did not throw an expected 
IllegalArguementException.");
+               } catch (IllegalArgumentException e) {
+
+               }
+       }
+
+       // 
-----------------------------------------------------------------------
+       public void testSetDays() throws Exception {
+               Date base = new Date(MILLIS_TEST);
+               Date result = DateUtils.setDays(base, 1);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 6, 1, 4, 3, 2, 1);
+
+               result = DateUtils.setDays(base, 29);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 6, 29, 4, 3, 2, 1);
+
+               try {
+                       result = DateUtils.setDays(base, 32);
+                       fail("DateUtils.setDays did not throw an expected 
IllegalArguementException.");
+               } catch (IllegalArgumentException e) {
+
+               }
+       }
+
+       // 
-----------------------------------------------------------------------
+       public void testSetHours() throws Exception {
+               Date base = new Date(MILLIS_TEST);
+               Date result = DateUtils.setHours(base, 0);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 6, 5, 0, 3, 2, 1);
+
+               result = DateUtils.setHours(base, 23);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 6, 5, 23, 3, 2, 1);
+
+               try {
+                       result = DateUtils.setHours(base, 24);
+                       fail("DateUtils.setHours did not throw an expected 
IllegalArguementException.");
+               } catch (IllegalArgumentException e) {
+
+               }
+       }
+
+       // 
-----------------------------------------------------------------------
+       public void testSetMinutes() throws Exception {
+               Date base = new Date(MILLIS_TEST);
+               Date result = DateUtils.setMinutes(base, 0);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 6, 5, 4, 0, 2, 1);
+
+               result = DateUtils.setMinutes(base, 59);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 6, 5, 4, 59, 2, 1);
+
+               try {
+                       result = DateUtils.setMinutes(base, 60);
+                       fail("DateUtils.setMinutes did not throw an expected 
IllegalArguementException.");
+               } catch (IllegalArgumentException e) {
+
+               }
+       }
+
+       // 
-----------------------------------------------------------------------
+       public void testSetSeconds() throws Exception {
+               Date base = new Date(MILLIS_TEST);
+               Date result = DateUtils.setSeconds(base, 0);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 6, 5, 4, 3, 0, 1);
+
+               result = DateUtils.setSeconds(base, 59);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 6, 5, 4, 3, 59, 1);
+
+               try {
+                       result = DateUtils.setSeconds(base, 60);
+                       fail("DateUtils.setSeconds did not throw an expected 
IllegalArguementException.");
+               } catch (IllegalArgumentException e) {
+
+               }
+       }
+
+       // 
-----------------------------------------------------------------------
+       public void testSetMilliseconds() throws Exception {
+               Date base = new Date(MILLIS_TEST);
+               Date result = DateUtils.setMilliseconds(base, 0);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 6, 5, 4, 3, 2, 0);
+
+               result = DateUtils.setMilliseconds(base, 999);
+               assertNotSame(base, result);
+               assertDate(base, 2000, 6, 5, 4, 3, 2, 1);
+               assertDate(result, 2000, 6, 5, 4, 3, 2, 999);
+
+               try {
+                       result = DateUtils.setMilliseconds(base, 1000);
+                       fail("DateUtils.setMilliseconds did not throw an 
expected IllegalArguementException.");
+               } catch (IllegalArgumentException e) {
+
+               }
+       }
 
     //-----------------------------------------------------------------------
     private void assertDate(Date date, int year, int month, int day, int hour, 
int min, int sec, int mil) throws Exception {


Reply via email to