From 1eecfc948bf6f4cbb9a2481313ca7368ec653056 Mon Sep 17 00:00:00 2001 From: Kaiyuan Wang Date: Tue, 20 Sep 2016 23:58:36 -0500 Subject: [PATCH 1/5] Add DateUtils.toCalendar(Date, TimeZone) --- .../org/apache/commons/lang3/time/DateUtils.java | 13 +++++++++++++ 1 file changed, 13 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 c242bd587..e9ae23f00 100644 --- a/src/main/java/org/apache/commons/lang3/time/DateUtils.java +++ b/src/main/java/org/apache/commons/lang3/time/DateUtils.java @@ -666,6 +666,19 @@ public class DateUtils { return c; } + //----------------------------------------------------------------------- + /** + * Converts a {@code Date} of a given {@code TimeZone} into a {@code Calendar} + * @param date the date to convert to a Calendar + * @param timeZone the time zone of the @{code date} + * @return + */ + public static Calendar toCalendar(final Date date, final TimeZone tz) { + final Calendar c = Calendar.getInstance(tz); + c.setTime(date); + return c; + } + //----------------------------------------------------------------------- /** *

Rounds a date, leaving the field specified as the most From ac5a216f767c6defa4da720c6ecb3baa05e30254 Mon Sep 17 00:00:00 2001 From: Kaiyuan Wang Date: Tue, 20 Sep 2016 23:59:04 -0500 Subject: [PATCH 2/5] Add unit tests for DateUtils.toCalendar(Date, TimeZone) --- .../commons/lang3/time/DateUtilsTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) 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 0b80ab633..280d681a6 100644 --- a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java @@ -693,6 +693,43 @@ public class DateUtilsTest { // expected } } + + //----------------------------------------------------------------------- + @Test + public void testToCalendarWithDate() { + assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the Date back", date1, DateUtils.toCalendar(date1, zone).getTime()); + try { + DateUtils.toCalendar(null, zone); + fail("Expected NullPointerException to be thrown"); + } catch(final NullPointerException npe) { + // expected + } + } + + //----------------------------------------------------------------------- + @Test + public void testToCalendarWithTimeZone() { + assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the TimeZone back", zone, DateUtils.toCalendar(date1, zone).getTimeZone()); + try { + DateUtils.toCalendar(date1, null); + fail("Expected NullPointerException to be thrown"); + } catch(final NullPointerException npe) { + // expected + } + } + + //----------------------------------------------------------------------- + @Test + public void testToCalendarWithDateAndTimeZone() { + try { + Calendar c = DateUtils.toCalendar(date2, defaultZone); + assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the Date back", date2, c.getTime()); + assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the TimeZone back", defaultZone, c.getTimeZone()); + // expected + } catch(final NullPointerException npe) { + fail("Expected NullPointerException to be thrown"); + } + } //----------------------------------------------------------------------- /** From d9a2c69a9d1db6072e1d7b7ea4fcbd5c15d20b5d Mon Sep 17 00:00:00 2001 From: Kaiyuan Wang Date: Sat, 24 Sep 2016 10:01:41 -0500 Subject: [PATCH 3/5] Handle Benedikt Ritter's comments --- .../apache/commons/lang3/time/DateUtils.java | 5 ++- .../commons/lang3/time/DateUtilsTest.java | 40 +++++++++++++------ 2 files changed, 31 insertions(+), 14 deletions(-) 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 e9ae23f00..8349c2583 100644 --- a/src/main/java/org/apache/commons/lang3/time/DateUtils.java +++ b/src/main/java/org/apache/commons/lang3/time/DateUtils.java @@ -670,8 +670,9 @@ public class DateUtils { /** * Converts a {@code Date} of a given {@code TimeZone} into a {@code Calendar} * @param date the date to convert to a Calendar - * @param timeZone the time zone of the @{code date} - * @return + * @param tz the time zone of the @{code date} + * @return the created Calendar + * @throws NullPointerException if {@code date} or {@code tz} is null */ public static Calendar toCalendar(final Date date, final TimeZone tz) { final Calendar c = Calendar.getInstance(tz); 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 280d681a6..d4d191703 100644 --- a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java @@ -696,11 +696,16 @@ public class DateUtilsTest { //----------------------------------------------------------------------- @Test - public void testToCalendarWithDate() { + public void testToCalendarWithDateNotNull() { assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the Date back", date1, DateUtils.toCalendar(date1, zone).getTime()); + } + + //----------------------------------------------------------------------- + @Test + public void testToCalendarWithDateNull() { try { DateUtils.toCalendar(null, zone); - fail("Expected NullPointerException to be thrown"); + fail("Expected NullPointerException to be thrown when Date is null"); } catch(final NullPointerException npe) { // expected } @@ -708,26 +713,37 @@ public class DateUtilsTest { //----------------------------------------------------------------------- @Test - public void testToCalendarWithTimeZone() { + public void testToCalendarWithTimeZoneNotNull() { assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the TimeZone back", zone, DateUtils.toCalendar(date1, zone).getTimeZone()); + } + + //----------------------------------------------------------------------- + @Test + public void testToCalendarWithTimeZoneNull() { try { DateUtils.toCalendar(date1, null); - fail("Expected NullPointerException to be thrown"); + fail("Expected NullPointerException to be thrown when TimeZone is null"); } catch(final NullPointerException npe) { // expected } } - //----------------------------------------------------------------------- + //----------------------------------------------------------------------- @Test - public void testToCalendarWithDateAndTimeZone() { - try { - Calendar c = DateUtils.toCalendar(date2, defaultZone); - assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the Date back", date2, c.getTime()); - assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the TimeZone back", defaultZone, c.getTimeZone()); - // expected + public void testToCalendarWithDateAndTimeZoneNotNull() { + Calendar c = DateUtils.toCalendar(date2, defaultZone); + assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the Date back", date2, c.getTime()); + assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the TimeZone back", defaultZone, c.getTimeZone()); + } + + //----------------------------------------------------------------------- + @Test + public void testToCalendarWithDateAndTimeZoneNull() { + try { + DateUtils.toCalendar(null, null); + fail("Expected NullPointerException to be thrown when both Date and TimeZone are null"); } catch(final NullPointerException npe) { - fail("Expected NullPointerException to be thrown"); + // expected } } From 8ac857c41ec8ae02e57eb0c1c1a012525e7e14b9 Mon Sep 17 00:00:00 2001 From: Kaiyuan Wang Date: Sun, 25 Sep 2016 13:13:03 -0500 Subject: [PATCH 4/5] Handle Comments --- .../commons/lang3/time/DateUtilsTest.java | 45 ++++--------------- 1 file changed, 9 insertions(+), 36 deletions(-) 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 d4d191703..bb6e59f55 100644 --- a/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java @@ -695,56 +695,29 @@ public class DateUtilsTest { } //----------------------------------------------------------------------- - @Test - public void testToCalendarWithDateNotNull() { - assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the Date back", date1, DateUtils.toCalendar(date1, zone).getTime()); - } - - //----------------------------------------------------------------------- - @Test + @Test(expected=NullPointerException.class) public void testToCalendarWithDateNull() { - try { - DateUtils.toCalendar(null, zone); - fail("Expected NullPointerException to be thrown when Date is null"); - } catch(final NullPointerException npe) { - // expected - } + DateUtils.toCalendar(null, zone); } //----------------------------------------------------------------------- - @Test - public void testToCalendarWithTimeZoneNotNull() { - assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the TimeZone back", zone, DateUtils.toCalendar(date1, zone).getTimeZone()); - } - - //----------------------------------------------------------------------- - @Test + @Test(expected=NullPointerException.class) public void testToCalendarWithTimeZoneNull() { - try { - DateUtils.toCalendar(date1, null); - fail("Expected NullPointerException to be thrown when TimeZone is null"); - } catch(final NullPointerException npe) { - // expected - } + DateUtils.toCalendar(date1, null); } //----------------------------------------------------------------------- @Test public void testToCalendarWithDateAndTimeZoneNotNull() { - Calendar c = DateUtils.toCalendar(date2, defaultZone); - assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the Date back", date2, c.getTime()); - assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the TimeZone back", defaultZone, c.getTimeZone()); + Calendar c = DateUtils.toCalendar(date2, defaultZone); + assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the Date back", date2, c.getTime()); + assertEquals("Convert Date and TimeZone to a Calendar, but failed to get the TimeZone back", defaultZone, c.getTimeZone()); } //----------------------------------------------------------------------- - @Test + @Test(expected=NullPointerException.class) public void testToCalendarWithDateAndTimeZoneNull() { - try { - DateUtils.toCalendar(null, null); - fail("Expected NullPointerException to be thrown when both Date and TimeZone are null"); - } catch(final NullPointerException npe) { - // expected - } + DateUtils.toCalendar(null, null); } //----------------------------------------------------------------------- From 9209e235a57d1b801b5da527b298f6387e311b34 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Sun, 2 Oct 2016 12:26:19 +0200 Subject: [PATCH 5/5] Add LANG-1255 to changes.xml --- src/changes/changes.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index bc720f759..8a9861e13 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -46,6 +46,7 @@ The type attribute can be add,update,fix,remove. + Add DateUtils.toCalendar(Date, TimeZone) ArrayUtils.contains returns false for instances of subtypes Prepare Java 9 detection Rename NumberUtils.isNumber, isCreatable to better reflect createNumber. Also, accommodated for "+" symbol as prefix in isCreatable and isNumber.