commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r1056840 - in /commons/proper/lang/branches/LANG_2_X/src: main/java/org/apache/commons/lang/time/DateUtils.java test/java/org/apache/commons/lang/time/DateUtilsTest.java
Date Sun, 09 Jan 2011 00:12:23 GMT
Author: niallp
Date: Sun Jan  9 00:12:23 2011
New Revision: 1056840

URL: http://svn.apache.org/viewvc?rev=1056840&view=rev
Log:
Port LANG-632 to LANG 2.x Branch - provide a Date to Calendar convenience method

Modified:
    commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/time/DateUtils.java
    commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/time/DateUtilsTest.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=1056840&r1=1056839&r2=1056840&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
Sun Jan  9 00:12:23 2011
@@ -659,6 +659,21 @@ public class DateUtils {
         c.set(calendarField, amount);
         return c.getTime();
     }   
+
+    //-----------------------------------------------------------------------
+    /**
+     * Convert a Date into a Calendar object. 
+     * 
+     * @param date the date to convert to a Calendar
+     * @return the created Calendar
+     * @throws NullPointerException if null is passed in
+     * @since 2.6
+     */
+    public static Calendar toCalendar(Date date) {
+        Calendar c = Calendar.getInstance();
+        c.setTime(date);
+        return c;
+    }
     
     //-----------------------------------------------------------------------
     /**

Modified: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/time/DateUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/time/DateUtilsTest.java?rev=1056840&r1=1056839&r2=1056840&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/time/DateUtilsTest.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/time/DateUtilsTest.java
Sun Jan  9 00:12:23 2011
@@ -617,6 +617,17 @@ public class DateUtilsTest extends TestC
     }
 
     //-----------------------------------------------------------------------
+    public void testToCalendar() {
+        assertEquals("Failed to convert to a Calendar and back", date1, DateUtils.toCalendar(date1).getTime());
+        try {
+            DateUtils.toCalendar(null);
+            fail("Expected NullPointerException to be thrown");
+        } catch(NullPointerException npe) {
+            // expected
+        }
+    }
+
+    //-----------------------------------------------------------------------
     /**
      * Tests various values with the round method
      */



Mime
View raw message