[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.
This commit is contained in:
Gary Gregory 2022-03-07 15:11:28 -05:00
parent 18bd973564
commit c82d9a4809
3 changed files with 23 additions and 7 deletions

View File

@ -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>

View File

@ -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();
}
}

View File

@ -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 {