Adding toCalendar method per LANG-632

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@963601 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2010-07-13 05:17:48 +00:00
parent 26cc12705e
commit 434575ed0a
2 changed files with 25 additions and 0 deletions

View File

@ -621,6 +621,20 @@ 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
*/
public static Calendar toCalendar(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
//-----------------------------------------------------------------------
/**

View File

@ -597,6 +597,17 @@ public class DateUtilsTest extends TestCase {
assertEquals(mil, cal.get(Calendar.MILLISECOND));
}
//-----------------------------------------------------------------------
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