You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/03/07 20:11:33 UTC

[commons-lang] branch master updated: [LANG-1462] Use TimeZone from calendar in DateFormatUtils.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new c82d9a4  [LANG-1462] Use TimeZone from calendar in DateFormatUtils.
c82d9a4 is described below

commit c82d9a4809a94280adb25cd46151bdf139518f1d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Mar 7 15:11:28 2022 -0500

    [LANG-1462] Use TimeZone from calendar in DateFormatUtils.
    
    Adapted from PR #863 by mbuiakova with:
    - No SpotBugs failure
    - Refactored common code
    - Moved new test method to proper location in test class.
---
 src/changes/changes.xml                              |  1 +
 .../apache/commons/lang3/time/DateFormatUtils.java   | 20 +++++++++++++-------
 .../commons/lang3/time/DateFormatUtilsTest.java      |  9 +++++++++
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b49b19a..abad55f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -69,6 +69,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory">ArrayUtils.toPrimitive(Boolean...) null array elements map to false, like Boolean.parseBoolean(null) and its callers return false.</action>
     <action                   type="fix" dev="ggregory" due-to="CodeQL, Gary Gregory">StrBuilder.StrBuilderReader.skip(long): Throw an exception when an implicit narrowing conversion in a compound assignment would result in information loss or a numeric error such as an overflows.</action>
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate Validate#notNull(Object) in favor of using Objects#requireNonNull(Object, String).</action>
+    <action issue="LANG-1462" type="fix" dev="ggregory" due-to="Lijun Liang, Arun Avanathan, Tai Dupree, Maria Buiakova, Gary Gregory">Use TimeZone from calendar in DateFormatUtils.</action>
     <!-- ADD -->
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add EnumUtils.getEnumSystemProperty(...).</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add TriConsumer.</action>
diff --git a/src/main/java/org/apache/commons/lang3/time/DateFormatUtils.java b/src/main/java/org/apache/commons/lang3/time/DateFormatUtils.java
index 7ec1163..820a6b3 100644
--- a/src/main/java/org/apache/commons/lang3/time/DateFormatUtils.java
+++ b/src/main/java/org/apache/commons/lang3/time/DateFormatUtils.java
@@ -271,7 +271,8 @@ public class DateFormatUtils {
     }
 
     /**
-     * <p>Formats a calendar into a specific pattern.</p>
+     * <p>Formats a calendar into a specific pattern. The TimeZone from the calendar
+     * will be used for formatting.</p>
      *
      * @param calendar  the calendar to format, not null
      * @param pattern  the pattern to use to format the calendar, not null
@@ -280,7 +281,7 @@ public class DateFormatUtils {
      * @since 2.4
      */
     public static String format(final Calendar calendar, final String pattern) {
-        return format(calendar, pattern, null, null);
+        return format(calendar, pattern, getTimeZone(calendar), null);
     }
 
     /**
@@ -346,7 +347,8 @@ public class DateFormatUtils {
     }
 
     /**
-     * <p>Formats a calendar into a specific pattern in a locale.</p>
+     * <p>Formats a calendar into a specific pattern in a locale. The TimeZone from the calendar
+     * will be used for formatting.</p>
      *
      * @param calendar  the calendar to format, not null
      * @param pattern  the pattern to use to format the calendar, not null
@@ -356,11 +358,11 @@ public class DateFormatUtils {
      * @since 2.4
      */
     public static String format(final Calendar calendar, final String pattern, final Locale locale) {
-        return format(calendar, pattern, null, locale);
+        return format(calendar, pattern, getTimeZone(calendar), locale);
     }
 
     /**
-     * <p>Formats a date/time into a specific pattern in a time zone  and locale.</p>
+     * <p>Formats a date/time into a specific pattern in a time zone and locale.</p>
      *
      * @param millis  the date to format expressed in milliseconds
      * @param pattern  the pattern to use to format the date, not null
@@ -373,7 +375,7 @@ public class DateFormatUtils {
     }
 
     /**
-     * <p>Formats a date/time into a specific pattern in a time zone  and locale.</p>
+     * <p>Formats a date/time into a specific pattern in a time zone and locale.</p>
      *
      * @param date  the date to format, not null
      * @param pattern  the pattern to use to format the date, not null, not null
@@ -387,7 +389,7 @@ public class DateFormatUtils {
     }
 
     /**
-     * <p>Formats a calendar into a specific pattern in a time zone  and locale.</p>
+     * <p>Formats a calendar into a specific pattern in a time zone and locale.</p>
      *
      * @param calendar  the calendar to format, not null
      * @param pattern  the pattern to use to format the calendar, not null
@@ -402,4 +404,8 @@ public class DateFormatUtils {
         return df.format(calendar);
     }
 
+    private static TimeZone getTimeZone(final Calendar calendar) {
+        return calendar == null ? null : calendar.getTimeZone();
+    }
+
 }
diff --git a/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java
index 0c78bb4..2719fc0 100644
--- a/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/DateFormatUtilsTest.java
@@ -150,6 +150,15 @@ public class DateFormatUtilsTest {
         DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.parse(date);
     }
 
+    @Test
+    public void testLANG1462() {
+        TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
+        Calendar calendar = createJuneTestDate(timeZone);
+        assertEquals("20030608101112", DateFormatUtils.format(calendar, "yyyyMMddHHmmss"));
+        calendar.setTimeZone(TimeZone.getTimeZone("JST"));
+        assertEquals("20030608221112", DateFormatUtils.format(calendar, "yyyyMMddHHmmss"));
+    }
+
     @DefaultTimeZone("UTC")
     @Test
     public void testLang530() throws ParseException {