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:
parent
26cc12705e
commit
434575ed0a
|
@ -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;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue