You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2008/02/01 08:12:59 UTC

svn commit: r617360 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/time/DateUtils.java test/org/apache/commons/lang/time/DateUtilsFragmentTest.java test/org/apache/commons/lang/time/TimeTestSuite.java

Author: bayard
Date: Thu Jan 31 23:12:56 2008
New Revision: 617360

URL: http://svn.apache.org/viewvc?rev=617360&view=rev
Log:
Applying my patch from LANG-379, based on Robert Scholte's patch adding the 'getFragment' family of methods

Added:
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsFragmentTest.java   (with props)
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/TimeTestSuite.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=617360&r1=617359&r2=617360&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 Thu Jan 31 23:12:56 2008
@@ -28,12 +28,23 @@
 /**
  * <p>A suite of utilities surrounding the use of the
  * {@link java.util.Calendar} and {@link java.util.Date} object.</p>
+ * 
+ * <p>DateUtils contains a lot of common methods considering manipulations of Dates or Calendars.
+ * Some methods require some extra explanation.
+ * The truncate and round methods could be considered the Math.floor(), Math.ceil() or Math.round versions for dates
+ * This way date-fields will be ignored in bottom-up order.
+ * As a complement to these methods we've introduced some fragment-methods. With these methods the Date-fields will be ignored in top-down order.
+ * Since a date without a year is not a valid date, you have to decide in what kind of date-field you want your result, for instance milliseconds or days.
+ * </p>
+ *   
+ *   
  *
  * @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
  * @author Stephen Colebourne
  * @author Janek Bogucki
  * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
  * @author Phil Steitz
+ * @author Robert Scholte
  * @since 2.0
  * @version $Id$
  */
@@ -1035,6 +1046,473 @@
         } else {
             throw new ClassCastException("Could not iterate based on " + focus);
         }
+    }
+    
+    /**
+     * <p>Returns the number of milliseconds within the 
+     * fragment. All datefields greater than the fragment will be ignored.</p>
+     * 
+     * <p>Asking the milliseconds of any date will only return the number of milliseconds
+     * of the current second (resulting in a number between 0 and 999). This 
+     * method will retrieve the number of milliseconds for any fragment. 
+     * For example, if you want to calculate the number of milliseconds past today, 
+     * your fragment is Calendar.DATE or Calendar.DAY_OF_YEAR. The result will
+     * be all milliseconds of the past hour(s), minutes(s) and second(s).</p>
+     * 
+     * <p>Valid fragments are: Calendar.YEAR, Calendar.MONTH, both 
+     * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, 
+     * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND
+     * A fragment less than or equal to a SECOND field will return 0.</p> 
+     * 
+     * <p>
+     * <ul>
+     *  <li>January 1, 2008 7:15:10.538 with Calendar.SECOND as fragment will return 538</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.SECOND as fragment will return 538</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10538 (10*1000 + 538)</li>
+     *  <li>January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 (a millisecond cannot be split in milliseconds)</li>
+     * </ul>
+     * </p>
+     * 
+     * @param date the date to work with, not null
+     * @param fragment the Calendar field part of date to calculate 
+     * @return number of milliseconds within the fragment of date
+     * @throws IllegalArgumentException if the date is <code>null</code> or
+     * fragment is not supported
+     * @since 2.4
+     */
+    public static long getFragmentInMilliseconds(Date date, int fragment) {
+        return getFragment(date, fragment, Calendar.MILLISECOND);    
+    }
+    
+    /**
+     * <p>Returns the number of seconds within the 
+     * fragment. All datefields greater than the fragment will be ignored.</p> 
+     * 
+     * <p>Asking the seconds of any date will only return the number of seconds
+     * of the current minute (resulting in a number between 0 and 59). This 
+     * method will retrieve the number of seconds for any fragment. 
+     * For example, if you want to calculate the number of seconds past today, 
+     * your fragment is Calendar.DATE or Calendar.DAY_OF_YEAR. The result will
+     * be all seconds of the past hour(s) and minutes(s).</p> 
+     * 
+     * <p>Valid fragments are: Calendar.YEAR, Calendar.MONTH, both 
+     * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, 
+     * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND
+     * A fragment less than or equal to a SECOND field will return 0.</p> 
+     * 
+     * <p>
+     * <ul>
+     *  <li>January 1, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10 (equivalent to deprecated date.getSeconds())</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10 (equivalent to deprecated date.getSeconds())</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 26110 (7*3600 + 15*60 + 10)</li>
+     *  <li>January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 (a millisecond cannot be split in seconds)</li>
+     * </ul>
+     * </p>
+     * 
+     * @param date the date to work with, not null
+     * @param fragment the Calendar field part of date to calculate 
+     * @return number of seconds within the fragment of date
+     * @throws IllegalArgumentException if the date is <code>null</code> or
+     * fragment is not supported
+     * @since 2.4
+     */
+    public static long getFragmentInSeconds(Date date, int fragment) {
+        return getFragment(date, fragment, Calendar.SECOND);
+    }
+    
+    /**
+     * <p>Returns the number of minutes within the 
+     * fragment. All datefields greater than the fragment will be ignored.</p> 
+     * 
+     * <p>Asking the minutes of any date will only return the number of minutes
+     * of the current hour (resulting in a number between 0 and 59). This 
+     * method will retrieve the number of minutes for any fragment. 
+     * For example, if you want to calculate the number of minutes past this month, 
+     * your fragment is Calendar.MONTH. The result will be all minutes of the 
+     * past day(s) and hour(s).</p> 
+     * 
+     * <p>Valid fragments are: Calendar.YEAR, Calendar.MONTH, both 
+     * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, 
+     * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND
+     * A fragment less than or equal to a MINUTE field will return 0.</p> 
+     * 
+     * <p>
+     * <ul>
+     *  <li>January 1, 2008 7:15:10.538 with Calendar.HOUR_OF_DAY as fragment will return 15 (equivalent to deprecated date.getMinutes())</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.HOUR_OF_DAY as fragment will return 15 (equivalent to deprecated date.getMinutes())</li>
+     *  <li>January 1, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 15</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 435 (7*60 + 15)</li>
+     *  <li>January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 (a millisecond cannot be split in minutes)</li>
+     * </ul>
+     * </p>
+     * 
+     * @param date the date to work with, not null
+     * @param fragment the Calendar field part of date to calculate 
+     * @return number of minutes within the fragment of date
+     * @throws IllegalArgumentException if the date is <code>null</code> or 
+     * fragment is not supported
+     * @since 2.4
+     */
+    public static long getFragmentInMinutes(Date date, int fragment) {
+        return getFragment(date, fragment, Calendar.MINUTE);
+    }
+    
+    /**
+     * <p>Returns the number of hours within the 
+     * fragment. All datefields greater than the fragment will be ignored.</p> 
+     * 
+     * <p>Asking the hours of any date will only return the number of hours
+     * of the current day (resulting in a number between 0 and 23). This 
+     * method will retrieve the number of hours for any fragment. 
+     * For example, if you want to calculate the number of hours past this month, 
+     * your fragment is Calendar.MONTH. The result will be all hours of the 
+     * past day(s).</p> 
+     * 
+     * <p>Valid fragments are: Calendar.YEAR, Calendar.MONTH, both 
+     * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, 
+     * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND
+     * A fragment less than or equal to a HOUR field will return 0.</p> 
+     * 
+     * <p>
+     * <ul>
+     *  <li>January 1, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 7 (equivalent to deprecated date.getHours())</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 7 (equivalent to deprecated date.getHours())</li>
+     *  <li>January 1, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 7</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 127 (5*24 + 7)</li>
+     *  <li>January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 (a millisecond cannot be split in hours)</li>
+     * </ul>
+     * </p>
+     * 
+     * @param date the date to work with, not null
+     * @param fragment the Calendar field part of date to calculate 
+     * @return number of hours within the fragment of date
+     * @throws IllegalArgumentException if the date is <code>null</code> or 
+     * fragment is not supported
+     * @since 2.4
+     */
+    public static long getFragmentInHours(Date date, int fragment) {
+        return getFragment(date, fragment, Calendar.HOUR_OF_DAY);
+    }
+    
+    /**
+     * <p>Returns the number of days within the 
+     * fragment. All datefields greater than the fragment will be ignored.</p> 
+     * 
+     * <p>Asking the days of any date will only return the number of days
+     * of the current month (resulting in a number between 1 and 31). This 
+     * method will retrieve the number of days for any fragment. 
+     * For example, if you want to calculate the number of days past this year, 
+     * your fragment is Calendar.YEAR. The result will be all days of the 
+     * past month(s).</p> 
+     * 
+     * <p>Valid fragments are: Calendar.YEAR, Calendar.MONTH, both 
+     * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, 
+     * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND
+     * A fragment less than or equal to a DAY field will return 0.</p> 
+     *  
+     * <p>
+     * <ul>
+     *  <li>January 28, 2008 with Calendar.MONTH as fragment will return 28 (equivalent to deprecated date.getDay())</li>
+     *  <li>February 28, 2008 with Calendar.MONTH as fragment will return 28 (equivalent to deprecated date.getDay())</li>
+     *  <li>January 28, 2008 with Calendar.YEAR as fragment will return 28</li>
+     *  <li>February 28, 2008 with Calendar.YEAR as fragment will return 59</li>
+     *  <li>January 28, 2008 with Calendar.MILLISECOND as fragment will return 0 (a millisecond cannot be split in days)</li>
+     * </ul>
+     * </p>
+     * 
+     * @param date the date to work with, not null
+     * @param fragment the Calendar field part of date to calculate 
+     * @return number of days  within the fragment of date
+     * @throws IllegalArgumentException if the date is <code>null</code> or 
+     * fragment is not supported
+     * @since 2.4
+     */
+    public static long getFragmentInDays(Date date, int fragment) {
+        return getFragment(date, fragment, Calendar.DAY_OF_YEAR);
+    }
+
+    /**
+     * <p>Returns the number of milliseconds within the 
+     * fragment. All datefields greater than the fragment will be ignored.</p> 
+     * 
+     * <p>Asking the milliseconds of any date will only return the number of milliseconds
+     * of the current second (resulting in a number between 0 and 999). This 
+     * method will retrieve the number of milliseconds for any fragment. 
+     * For example, if you want to calculate the number of seconds past today, 
+     * your fragment is Calendar.DATE or Calendar.DAY_OF_YEAR. The result will
+     * be all seconds of the past hour(s), minutes(s) and second(s).</p> 
+     * 
+     * <p>Valid fragments are: Calendar.YEAR, Calendar.MONTH, both 
+     * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, 
+     * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND
+     * A fragment less than or equal to a MILLISECOND field will return 0.</p> 
+     * 
+     * <p>
+     * <ul>
+     *  <li>January 1, 2008 7:15:10.538 with Calendar.SECOND as fragment will return 538 (equivalent to calendar.get(Calendar.MILLISECOND))</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.SECOND as fragment will return 538 (equivalent to calendar.get(Calendar.MILLISECOND))</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10538 (10*1000 + 538)</li>
+     *  <li>January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 (a millisecond cannot be split in milliseconds)</li>
+     * </ul>
+     * </p>
+     * 
+     * @param calendar the calendar to work with, not null
+     * @param fragment the Calendar field part of calendar to calculate 
+     * @return number of milliseconds within the fragment of date
+     * @throws IllegalArgumentException if the date is <code>null</code> or 
+     * fragment is not supported
+     * @since 2.4
+     */
+  public static long getFragmentInMilliseconds(Calendar calendar, int fragment) {
+    return getFragment(calendar, fragment, Calendar.MILLISECOND);
+  }
+    /**
+     * <p>Returns the number of seconds within the 
+     * fragment. All datefields greater than the fragment will be ignored.</p> 
+     * 
+     * <p>Asking the seconds of any date will only return the number of seconds
+     * of the current minute (resulting in a number between 0 and 59). This 
+     * method will retrieve the number of seconds for any fragment. 
+     * For example, if you want to calculate the number of seconds past today, 
+     * your fragment is Calendar.DATE or Calendar.DAY_OF_YEAR. The result will
+     * be all seconds of the past hour(s) and minutes(s).</p> 
+     * 
+     * <p>Valid fragments are: Calendar.YEAR, Calendar.MONTH, both 
+     * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, 
+     * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND
+     * A fragment less than or equal to a SECOND field will return 0.</p> 
+     * 
+     * <p>
+     * <ul>
+     *  <li>January 1, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10 (equivalent to calendar.get(Calendar.SECOND))</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.MINUTE as fragment will return 10 (equivalent to calendar.get(Calendar.SECOND))</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 26110 (7*3600 + 15*60 + 10)</li>
+     *  <li>January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 (a millisecond cannot be split in seconds)</li>
+     * </ul>
+     * </p>
+     * 
+     * @param calendar the calendar to work with, not null
+     * @param fragment the Calendar field part of calendar to calculate 
+     * @return number of seconds within the fragment of date
+     * @throws IllegalArgumentException if the date is <code>null</code> or 
+     * fragment is not supported
+     * @since 2.4
+     */
+    public static long getFragmentInSeconds(Calendar calendar, int fragment) {
+        return getFragment(calendar, fragment, Calendar.SECOND);
+    }
+    
+    /**
+     * <p>Returns the number of minutes within the 
+     * fragment. All datefields greater than the fragment will be ignored.</p> 
+     * 
+     * <p>Asking the minutes of any date will only return the number of minutes
+     * of the current hour (resulting in a number between 0 and 59). This 
+     * method will retrieve the number of minutes for any fragment. 
+     * For example, if you want to calculate the number of minutes past this month, 
+     * your fragment is Calendar.MONTH. The result will be all minutes of the 
+     * past day(s) and hour(s).</p> 
+     * 
+     * <p>Valid fragments are: Calendar.YEAR, Calendar.MONTH, both 
+     * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, 
+     * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND
+     * A fragment less than or equal to a MINUTE field will return 0.</p> 
+     * 
+     * <p>
+     * <ul>
+     *  <li>January 1, 2008 7:15:10.538 with Calendar.HOUR_OF_DAY as fragment will return 15 (equivalent to calendar.get(Calendar.MINUTES))</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.HOUR_OF_DAY as fragment will return 15 (equivalent to calendar.get(Calendar.MINUTES))</li>
+     *  <li>January 1, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 15</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 435 (7*60 + 15)</li>
+     *  <li>January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 (a millisecond cannot be split in minutes)</li>
+     * </ul>
+     * </p>
+     * 
+     * @param calendar the calendar to work with, not null
+     * @param fragment the Calendar field part of calendar to calculate 
+     * @return number of minutes within the fragment of date
+     * @throws IllegalArgumentException if the date is <code>null</code> or 
+     * fragment is not supported
+     * @since 2.4
+     */
+    public static long getFragmentInMinutes(Calendar calendar, int fragment) {
+        return getFragment(calendar, fragment, Calendar.MINUTE);
+    }
+    
+    /**
+     * <p>Returns the number of hours within the 
+     * fragment. All datefields greater than the fragment will be ignored.</p> 
+     * 
+     * <p>Asking the hours of any date will only return the number of hours
+     * of the current day (resulting in a number between 0 and 23). This 
+     * method will retrieve the number of hours for any fragment. 
+     * For example, if you want to calculate the number of hours past this month, 
+     * your fragment is Calendar.MONTH. The result will be all hours of the 
+     * past day(s).</p> 
+     * 
+     * <p>Valid fragments are: Calendar.YEAR, Calendar.MONTH, both 
+     * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, 
+     * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND
+     * A fragment less than or equal to a HOUR field will return 0.</p> 
+     *  
+     * <p>
+     * <ul>
+     *  <li>January 1, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 7 (equivalent to calendar.get(Calendar.HOUR_OF_DAY))</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.DAY_OF_YEAR as fragment will return 7 (equivalent to calendar.get(Calendar.HOUR_OF_DAY))</li>
+     *  <li>January 1, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 7</li>
+     *  <li>January 6, 2008 7:15:10.538 with Calendar.MONTH as fragment will return 127 (5*24 + 7)</li>
+     *  <li>January 16, 2008 7:15:10.538 with Calendar.MILLISECOND as fragment will return 0 (a millisecond cannot be split in hours)</li>
+     * </ul>
+     * </p>
+     *  
+     * @param calendar the calendar to work with, not null
+     * @param fragment the Calendar field part of calendar to calculate 
+     * @return number of hours within the fragment of date
+     * @throws IllegalArgumentException if the date is <code>null</code> or 
+     * fragment is not supported
+     * @since 2.4
+     */
+    public static long getFragmentInHours(Calendar calendar, int fragment) {
+        return getFragment(calendar, fragment, Calendar.HOUR_OF_DAY);
+    }
+    
+    /**
+     * <p>Returns the number of days within the 
+     * fragment. All datefields greater than the fragment will be ignored.</p> 
+     * 
+     * <p>Asking the days of any date will only return the number of days
+     * of the current month (resulting in a number between 1 and 31). This 
+     * method will retrieve the number of days for any fragment. 
+     * For example, if you want to calculate the number of days past this year, 
+     * your fragment is Calendar.YEAR. The result will be all days of the 
+     * past month(s).</p> 
+     * 
+     * <p>Valid fragments are: Calendar.YEAR, Calendar.MONTH, both 
+     * Calendar.DAY_OF_YEAR and Calendar.DATE, Calendar.HOUR_OF_DAY, 
+     * Calendar.MINUTE, Calendar.SECOND and Calendar.MILLISECOND
+     * A fragment less than or equal to a DAY field will return 0.</p> 
+     * 
+     * <p>
+     * <ul>
+     *  <li>January 28, 2008 with Calendar.MONTH as fragment will return 28 (equivalent to calendar.get(Calendar.DAY_OF_MONTH))</li>
+     *  <li>February 28, 2008 with Calendar.MONTH as fragment will return 28 (equivalent to calendar.get(Calendar.DAY_OF_MONTH))</li>
+     *  <li>January 28, 2008 with Calendar.YEAR as fragment will return 28 (equivalent to calendar.get(Calendar.DAY_OF_YEAR))</li>
+     *  <li>February 28, 2008 with Calendar.YEAR as fragment will return 59 (equivalent to calendar.get(Calendar.DAY_OF_YEAR))</li>
+     *  <li>January 28, 2008 with Calendar.MILLISECOND as fragment will return 0 (a millisecond cannot be split in days)</li>
+     * </ul>
+     * </p>
+     * 
+     * @param calendar the calendar to work with, not null
+     * @param fragment the Calendar field part of calendar to calculate 
+     * @return number of days within the fragment of date
+     * @throws IllegalArgumentException if the date is <code>null</code> or 
+     * fragment is not supported
+     * @since 2.4
+     */
+    public static long getFragmentInDays(Calendar calendar, int fragment) {
+        return getFragment(calendar, fragment, Calendar.DAY_OF_YEAR);
+    }
+    
+    /**
+     * Date-version for fragment-calculation in any unit
+     * 
+     * @param date the date to work with, not null
+     * @param fragment the Calendar field part of date to calculate 
+     * @param unit Calendar field defining the unit
+     * @return number of units within the fragment of the date
+     * @throws IllegalArgumentException if the date is <code>null</code> or 
+     * fragment is not supported
+     * @since 2.4
+     */
+    private static long getFragment(Date date, int fragment, int unit) {
+        if(date == null) {
+            throw  new IllegalArgumentException("The date must not be null");
+        }
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        return getFragment(calendar, fragment, unit);
+    }
+
+    /**
+     * Calendar-version for fragment-calculation in any unit
+     * 
+     * @param calendar the calendar to work with, not null
+     * @param fragment the Calendar field part of calendar to calculate 
+     * @param unit Calendar field defining the unit
+     * @return number of units within the fragment of the calendar
+     * @throws IllegalArgumentException if the date is <code>null</code> or 
+     * fragment is not supported
+     * @since 2.4
+     */
+    private static long getFragment(Calendar calendar, int fragment, int unit) {
+        if(calendar == null) {
+            throw  new IllegalArgumentException("The date must not be null"); 
+        }
+        long millisPerUnit = getMillisPerUnit(unit);
+        long result = 0;
+        
+        // Fragments bigger than a day require a breakdown to days
+        switch (fragment) {
+            case Calendar.YEAR:
+                result += (calendar.get(Calendar.DAY_OF_YEAR) * MILLIS_PER_DAY) / millisPerUnit;
+                break;
+            case Calendar.MONTH:
+                result += (calendar.get(Calendar.DAY_OF_MONTH) * MILLIS_PER_DAY) / millisPerUnit;
+                break;
+        }
+
+        switch (fragment) {
+            // Number of days already calculated for these cases
+            case Calendar.YEAR:
+            case Calendar.MONTH:
+            
+            // The rest of the valid cases
+            case Calendar.DAY_OF_YEAR:
+            case Calendar.DATE:
+                result += (calendar.get(Calendar.HOUR_OF_DAY) * MILLIS_PER_HOUR) / millisPerUnit;
+            case Calendar.HOUR_OF_DAY:
+                result += (calendar.get(Calendar.MINUTE) * MILLIS_PER_MINUTE) / millisPerUnit;
+            case Calendar.MINUTE:
+                result += (calendar.get(Calendar.SECOND) * MILLIS_PER_SECOND) / millisPerUnit;
+            case Calendar.SECOND:
+                result += (calendar.get(Calendar.MILLISECOND) * 1) / millisPerUnit;
+                break;
+            case Calendar.MILLISECOND: break;//never useful
+                default: throw new IllegalArgumentException("The fragment " + fragment + " is not supported");
+        }
+        return result;
+    }
+    
+    /**
+     * 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
+     * @return number of millis
+     * @throws IllegalArgumentException if date can't be represented in millisenconds
+     * @since 2.4 
+     */
+    private static long getMillisPerUnit(int unit) {
+        long result = Long.MAX_VALUE;
+        switch (unit) {
+            case Calendar.DAY_OF_YEAR:
+            case Calendar.DATE:
+                result = MILLIS_PER_DAY;
+                break;
+            case Calendar.HOUR_OF_DAY:
+                result = MILLIS_PER_HOUR;
+                break;
+            case Calendar.MINUTE:
+                result = MILLIS_PER_MINUTE;
+                break;
+            case Calendar.SECOND:
+                result = MILLIS_PER_SECOND;
+                break;
+            case Calendar.MILLISECOND:
+                result = 1;
+                break;
+            default: throw new IllegalArgumentException("The unit " + unit + " cannot be represented is milleseconds");
+        }
+        return result;
     }
 
     /**

Added: commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsFragmentTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsFragmentTest.java?rev=617360&view=auto
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsFragmentTest.java (added)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsFragmentTest.java Thu Jan 31 23:12:56 2008
@@ -0,0 +1,491 @@
+package org.apache.commons.lang.time;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import junit.framework.TestCase;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.time.DateFormatUtils;
+
+public class DateUtilsFragmentTest extends TestCase {
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite(DateUtilsFragmentTest.class);
+        suite.setName("DateUtils Fragment Tests");
+        return suite;
+    }
+
+	private static final int months = 7;   // second final prime before 12
+	private static final int days = 23;    // second final prime before 31 (and valid)
+	private static final int hours = 19;   // second final prime before 24
+	private static final int minutes = 53; // second final prime before 60
+	private static final int seconds = 47; // third final prime before 60
+	private static final int millis = 991; // second final prime before 1000
+
+	private Date aDate;
+	private Calendar aCalendar;
+
+	protected void setUp() {
+		aCalendar = Calendar.getInstance();
+		aCalendar.set(2005, months, days, hours, minutes, seconds);
+		aCalendar.set(Calendar.MILLISECOND, millis);
+		aDate = aCalendar.getTime();
+	}
+	
+	public void testNullDate() {
+		try {
+			DateUtils.getFragmentInMilliseconds((Date) null, Calendar.MILLISECOND);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInSeconds((Date) null, Calendar.MILLISECOND);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInMinutes((Date) null, Calendar.MILLISECOND);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInHours((Date) null, Calendar.MILLISECOND);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInDays((Date) null, Calendar.MILLISECOND);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+	}
+
+	public void testNullCalendar() {
+		try {
+			DateUtils.getFragmentInMilliseconds((Calendar) null, Calendar.MILLISECOND);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInSeconds((Calendar) null, Calendar.MILLISECOND);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInMinutes((Calendar) null, Calendar.MILLISECOND);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInHours((Calendar) null, Calendar.MILLISECOND);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInDays((Calendar) null, Calendar.MILLISECOND);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+	}
+	
+	public void testInvalidFragmentWithDate() {
+		try {
+			DateUtils.getFragmentInMilliseconds(aDate, 0);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInSeconds(aDate, 0);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInMinutes(aDate, 0);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInHours(aDate, 0);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInDays(aDate, 0);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+	}
+
+	public void testInvalidFragmentWithCalendar() {
+		try {
+			DateUtils.getFragmentInMilliseconds(aCalendar, 0);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInSeconds(aCalendar, 0);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInMinutes(aCalendar, 0);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInHours(aCalendar, 0);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+
+		try {
+			DateUtils.getFragmentInDays(aCalendar, 0);
+			fail();
+		} catch(IllegalArgumentException iae) {}
+	}
+
+	public void testMillisecondFragmentInLargerUnitWithDate() {
+		assertEquals(0, DateUtils.getFragmentInMilliseconds(aDate, Calendar.MILLISECOND));
+		assertEquals(0, DateUtils.getFragmentInSeconds(aDate, Calendar.MILLISECOND));
+		assertEquals(0, DateUtils.getFragmentInMinutes(aDate, Calendar.MILLISECOND));
+		assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.MILLISECOND));
+		assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.MILLISECOND));
+	}
+
+	public void testMillisecondFragmentInLargerUnitWithCalendar() {
+		assertEquals(0, DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MILLISECOND));
+		assertEquals(0, DateUtils.getFragmentInSeconds(aCalendar, Calendar.MILLISECOND));
+		assertEquals(0, DateUtils.getFragmentInMinutes(aCalendar, Calendar.MILLISECOND));
+		assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.MILLISECOND));
+		assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.MILLISECOND));
+	}
+	
+	public void testSecondFragmentInLargerUnitWithDate() {
+		assertEquals(0, DateUtils.getFragmentInSeconds(aDate, Calendar.SECOND));
+		assertEquals(0, DateUtils.getFragmentInMinutes(aDate, Calendar.SECOND));
+		assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.SECOND));
+		assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.SECOND));
+	}
+
+	public void testSecondFragmentInLargerUnitWithCalendar() {
+		assertEquals(0, DateUtils.getFragmentInSeconds(aCalendar, Calendar.SECOND));
+		assertEquals(0, DateUtils.getFragmentInMinutes(aCalendar, Calendar.SECOND));
+		assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.SECOND));
+		assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.SECOND));
+	}
+	
+	public void testMinuteFragmentInLargerUnitWithDate() {
+		assertEquals(0, DateUtils.getFragmentInMinutes(aDate, Calendar.MINUTE));
+		assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.MINUTE));
+		assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.MINUTE));
+	}
+
+	public void testMinuteFragmentInLargerUnitWithCalendar() {
+		assertEquals(0, DateUtils.getFragmentInMinutes(aCalendar, Calendar.MINUTE));
+		assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.MINUTE));
+		assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.MINUTE));
+	}
+
+	public void testHourOfDayFragmentInLargerUnitWithDate() {
+		assertEquals(0, DateUtils.getFragmentInHours(aDate, Calendar.HOUR_OF_DAY));
+		assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.HOUR_OF_DAY));
+	}
+
+	public void testHourOfDayFragmentInLargerUnitWithCalendar() {
+		assertEquals(0, DateUtils.getFragmentInHours(aCalendar, Calendar.HOUR_OF_DAY));
+		assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.HOUR_OF_DAY));
+	}
+
+	public void testDayOfYearFragmentInLargerUnitWithDate() {
+		assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.DAY_OF_YEAR));
+	}
+
+	public void testDayOfYearFragmentInLargerUnitWithCalendar() {
+		assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.DAY_OF_YEAR));
+	}
+
+	public void testDateFragmentInLargerUnitWithDate() {
+		assertEquals(0, DateUtils.getFragmentInDays(aDate, Calendar.DATE));
+	}
+
+	public void testDateFragmentInLargerUnitWithCalendar() {
+		assertEquals(0, DateUtils.getFragmentInDays(aCalendar, Calendar.DATE));
+	}
+
+	//Calendar.SECOND as useful fragment
+	
+	public void testMillisecondsOfSecondWithDate() {
+		long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.SECOND);
+		assertEquals(millis, testResult);
+	}
+
+	public void testMillisecondsOfSecondWithCalendar() {
+		long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.SECOND);
+		assertEquals(millis, testResult);
+		assertEquals(aCalendar.get(Calendar.MILLISECOND), testResult);
+	}
+
+	//Calendar.MINUTE as useful fragment
+
+	public void testMillisecondsOfMinuteWithDate() {
+		long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.MINUTE);
+		assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND), testResult);
+	}
+
+	public void testMillisecondsOfMinuteWithCalender() {
+		long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MINUTE);
+		assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND), testResult);
+	}
+
+	public void testSecondsofMinuteWithDate() {
+		long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.MINUTE);
+		assertEquals(seconds, testResult);
+	}
+
+	public void testSecondsofMinuteWithCalendar() {
+		long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.MINUTE);
+		assertEquals(seconds, testResult);
+		assertEquals(aCalendar.get(Calendar.SECOND), testResult);
+	}
+
+	//Calendar.HOUR_OF_DAY as useful fragment
+	
+	public void testMillisecondsOfHourWithDate() {
+		long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.HOUR_OF_DAY);
+		assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE), testResult);
+	}
+	
+	public void testMillisecondsOfHourWithCalendar() {
+		long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.HOUR_OF_DAY);
+		assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE), testResult);
+	}
+
+	public void testSecondsofHourWithDate() {
+		long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.HOUR_OF_DAY);
+		assertEquals(
+				seconds
+						+ (minutes
+								* DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND),
+				testResult);
+	}
+
+	public void testSecondsofHourWithCalendar() {
+		long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.HOUR_OF_DAY);
+		assertEquals(
+				seconds
+						+ (minutes
+								* DateUtils.MILLIS_PER_MINUTE / DateUtils.MILLIS_PER_SECOND),
+				testResult);
+	}
+
+	public void testMinutesOfHourWithDate() {
+		long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.HOUR_OF_DAY);
+		assertEquals(minutes, testResult);
+	}
+
+	public void testMinutesOfHourWithCalendar() {
+		long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.HOUR_OF_DAY);
+		assertEquals(minutes, testResult);
+	}
+
+	//Calendar.DATE and Calendar.DAY_OF_YEAR as useful fragment
+	public void testMillisecondsOfDayWithDate() {
+		long testresult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.DATE);
+		long expectedValue = millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) + (hours * DateUtils.MILLIS_PER_HOUR); 
+		assertEquals(expectedValue, testresult);
+		testresult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.DAY_OF_YEAR);
+		assertEquals(expectedValue, testresult);
+	}
+	
+	public void testMillisecondsOfDayWithCalendar() {
+		long testresult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.DATE);
+		long expectedValue = millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE) + (hours * DateUtils.MILLIS_PER_HOUR); 
+		assertEquals(expectedValue, testresult);
+		testresult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.DAY_OF_YEAR);
+		assertEquals(expectedValue, testresult);
+	}
+
+	public void testSecondsOfDayWithDate() {
+		long testresult = DateUtils.getFragmentInSeconds(aDate, Calendar.DATE);
+		long expectedValue = seconds + ((minutes * DateUtils.MILLIS_PER_MINUTE) + (hours * DateUtils.MILLIS_PER_HOUR))/ DateUtils.MILLIS_PER_SECOND;
+		assertEquals(expectedValue, testresult);
+		testresult = DateUtils.getFragmentInSeconds(aDate, Calendar.DAY_OF_YEAR);
+		assertEquals(expectedValue, testresult);
+	}
+
+	public void testSecondsOfDayWithCalendar() {
+		long testresult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.DATE);
+		long expectedValue = seconds + ((minutes * DateUtils.MILLIS_PER_MINUTE) + (hours * DateUtils.MILLIS_PER_HOUR))/ DateUtils.MILLIS_PER_SECOND;
+		assertEquals(expectedValue, testresult);
+		testresult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.DAY_OF_YEAR);
+		assertEquals(expectedValue, testresult);
+	}
+
+	public void testMinutesOfDayWithDate() {
+		long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.DATE);
+		long expectedValue = minutes + ((hours * DateUtils.MILLIS_PER_HOUR))/ DateUtils.MILLIS_PER_MINUTE; 
+		assertEquals(expectedValue,testResult);
+		testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.DAY_OF_YEAR);
+		assertEquals(expectedValue,testResult);
+	}
+
+	public void testMinutesOfDayWithCalendar() {
+		long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.DATE);
+		long expectedValue = minutes + ((hours * DateUtils.MILLIS_PER_HOUR))/ DateUtils.MILLIS_PER_MINUTE; 
+		assertEquals(expectedValue, testResult);
+		testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.DAY_OF_YEAR);
+		assertEquals(expectedValue, testResult);
+	}
+	
+	public void testHoursOfDayWithDate() {
+		long testResult = DateUtils.getFragmentInHours(aDate, Calendar.DATE);
+		long expectedValue = hours; 
+		assertEquals(expectedValue,testResult);
+		testResult = DateUtils.getFragmentInHours(aDate, Calendar.DAY_OF_YEAR);
+		assertEquals(expectedValue,testResult);
+	}
+
+	public void testHoursOfDayWithCalendar() {
+		long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.DATE);
+		long expectedValue = hours; 
+		assertEquals(expectedValue, testResult);
+		testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.DAY_OF_YEAR);
+		assertEquals(expectedValue, testResult);
+	}
+	
+	
+	//Calendar.MONTH as useful fragment
+	public void testMillisecondsOfMonthWithDate() {
+		long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.MONTH);
+		assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE)
+								+ (hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY),
+				testResult);
+	}
+
+	public void testMillisecondsOfMonthWithCalendar() {
+		long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.MONTH);
+		assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE)
+				+ (hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY),
+testResult);
+	}
+	
+	public void testSecondsOfMonthWithDate() {
+		long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.MONTH);
+		assertEquals(
+				seconds
+						+ ((minutes * DateUtils.MILLIS_PER_MINUTE)
+								+ (hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY))
+						/ DateUtils.MILLIS_PER_SECOND,
+				testResult);
+	}
+
+	public void testSecondsOfMonthWithCalendar() {
+		long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.MONTH);
+		assertEquals(
+				seconds
+						+ ((minutes * DateUtils.MILLIS_PER_MINUTE)
+								+ (hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY))
+						/ DateUtils.MILLIS_PER_SECOND,
+				testResult);
+	}
+
+	public void testMinutesOfMonthWithDate() {
+		long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.MONTH);
+		assertEquals(minutes
+								+ ((hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY))
+						/ DateUtils.MILLIS_PER_MINUTE,
+				testResult);
+	}
+
+	public void testMinutesOfMonthWithCalendar() {
+		long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.MONTH);
+		assertEquals( minutes  +((hours * DateUtils.MILLIS_PER_HOUR) + (days * DateUtils.MILLIS_PER_DAY))
+						/ DateUtils.MILLIS_PER_MINUTE,
+				testResult);
+	}
+
+	public void testHoursOfMonthWithDate() {
+		long testResult = DateUtils.getFragmentInHours(aDate, Calendar.MONTH);
+		assertEquals(hours + ((days * DateUtils.MILLIS_PER_DAY))
+						/ DateUtils.MILLIS_PER_HOUR,
+				testResult);
+	}
+
+	public void testHoursOfMonthWithCalendar() {
+		long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.MONTH);
+		assertEquals( hours +((days * DateUtils.MILLIS_PER_DAY))
+						/ DateUtils.MILLIS_PER_HOUR,
+				testResult);
+	}
+	
+	//Calendar.YEAR as useful fragment
+	public void testMillisecondsOfYearWithDate() {
+		long testResult = DateUtils.getFragmentInMilliseconds(aDate, Calendar.YEAR);
+		Calendar cal = Calendar.getInstance();
+		cal.setTime(aDate);
+		assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE)
+								+ (hours * DateUtils.MILLIS_PER_HOUR) + (cal.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY),
+				testResult);
+	}
+
+	public void testMillisecondsOfYearWithCalendar() {
+		long testResult = DateUtils.getFragmentInMilliseconds(aCalendar, Calendar.YEAR);
+		assertEquals(millis + (seconds * DateUtils.MILLIS_PER_SECOND) + (minutes * DateUtils.MILLIS_PER_MINUTE)
+				+ (hours * DateUtils.MILLIS_PER_HOUR) + (aCalendar.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY),
+testResult);
+	}
+	
+	public void testSecondsOfYearWithDate() {
+		long testResult = DateUtils.getFragmentInSeconds(aDate, Calendar.YEAR);
+		Calendar cal = Calendar.getInstance();
+		cal.setTime(aDate);
+		assertEquals(
+				seconds
+						+ ((minutes * DateUtils.MILLIS_PER_MINUTE)
+								+ (hours * DateUtils.MILLIS_PER_HOUR) + (cal.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY))
+						/ DateUtils.MILLIS_PER_SECOND,
+				testResult);
+	}
+
+	public void testSecondsOfYearWithCalendar() {
+		long testResult = DateUtils.getFragmentInSeconds(aCalendar, Calendar.YEAR);
+		assertEquals(
+				seconds
+						+ ((minutes * DateUtils.MILLIS_PER_MINUTE)
+								+ (hours * DateUtils.MILLIS_PER_HOUR) + (aCalendar.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY))
+						/ DateUtils.MILLIS_PER_SECOND,
+				testResult);
+	}
+
+	public void testMinutesOfYearWithDate() {
+		long testResult = DateUtils.getFragmentInMinutes(aDate, Calendar.YEAR);
+		Calendar cal = Calendar.getInstance();
+		cal.setTime(aDate);
+		assertEquals(minutes
+								+ ((hours * DateUtils.MILLIS_PER_HOUR) + (cal.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY))
+						/ DateUtils.MILLIS_PER_MINUTE,
+				testResult);
+	}
+
+	public void testMinutesOfYearWithCalendar() {
+		long testResult = DateUtils.getFragmentInMinutes(aCalendar, Calendar.YEAR);
+		assertEquals( minutes  +((hours * DateUtils.MILLIS_PER_HOUR) + (aCalendar.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY))
+						/ DateUtils.MILLIS_PER_MINUTE,
+				testResult);
+	}
+
+	public void testHoursOfYearWithDate() {
+		long testResult = DateUtils.getFragmentInHours(aDate, Calendar.YEAR);
+		Calendar cal = Calendar.getInstance();
+		cal.setTime(aDate);
+		assertEquals(hours + ((cal.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY))
+						/ DateUtils.MILLIS_PER_HOUR,
+				testResult);
+	}
+
+	public void testHoursOfYearWithCalendar() {
+		long testResult = DateUtils.getFragmentInHours(aCalendar, Calendar.YEAR);
+		assertEquals( hours +((aCalendar.get(Calendar.DAY_OF_YEAR) * DateUtils.MILLIS_PER_DAY))
+						/ DateUtils.MILLIS_PER_HOUR,
+				testResult);
+	}
+}

Propchange: commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateUtilsFragmentTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/TimeTestSuite.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/TimeTestSuite.java?rev=617360&r1=617359&r2=617360&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/TimeTestSuite.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/TimeTestSuite.java Thu Jan 31 23:12:56 2008
@@ -54,6 +54,7 @@
         suite.addTest(DurationFormatUtilsTest.suite());
         suite.addTest(StopWatchTest.suite());
         suite.addTest(FastDateFormatTest.suite());
+        suite.addTest(DateUtilsFragmentTest.suite());
         return suite;
     }
 }