From 434575ed0a71439b8abe8ef59948bf15847fd9c0 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Tue, 13 Jul 2010 05:17:48 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/lang3/time/DateUtils.java | 14 ++++++++++++++ .../apache/commons/lang3/time/DateUtilsTest.java | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/time/DateUtils.java b/src/main/java/org/apache/commons/lang3/time/DateUtils.java index 3cd4b4bbc..f75d7bf48 100644 --- a/src/main/java/org/apache/commons/lang3/time/DateUtils.java +++ b/src/main/java/org/apache/commons/lang3/time/DateUtils.java @@ -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; + } //----------------------------------------------------------------------- /** diff --git a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java index f01ed28a7..cfb42219b 100644 --- a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java @@ -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