From 7dda350aab833c5e68943c5f8806bd1670622b46 Mon Sep 17 00:00:00 2001 From: Kittrell Date: Fri, 16 Feb 2024 14:12:18 -0500 Subject: [PATCH] Fixed to properly format dates having years with fewer than four digits. --- changelog.md | 2 ++ .../util/SafeSimpleDateFormat.java | 4 ++- .../util/TestSimpleDateFormat.java | 28 ++++++++++++++----- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/changelog.md b/changelog.md index 7fd94605..ebd0a053 100644 --- a/changelog.md +++ b/changelog.md @@ -1,4 +1,6 @@ ### Revision History +* 2.4.2 + * Fixed `SafeSimpleDateFormat` to properly format dates having years with fewer than four digits. * 2.4.1 * `Converter` has had significant expansion in the types that it can convert between, greater than 500 combinations. In addition, you can add your own conversions to it as well. Call the `Converter.getSupportedConversions()` to see all the combinations supported. Also, you can use `Converter` instance-based now, allowing it to have different conversion tables if needed. * `DateUtilities` has had performance improvements (> 35%), and adds a new `.parseDate()` API that allows it to return a `ZonedDateTime.` See the updated Javadoc on the class for a complete description of all of the formats it supports. Normally, you do not need to use this class directly, as you can use `Converter` to convert between `Dates`, `Calendars`, and the new Temporal classes like `ZonedDateTime,` `Duration,` `Instance,` as well as `long,` `BigInteger,` etc. diff --git a/src/main/java/com/cedarsoftware/util/SafeSimpleDateFormat.java b/src/main/java/com/cedarsoftware/util/SafeSimpleDateFormat.java index 94e016d4..1ae3b2d9 100644 --- a/src/main/java/com/cedarsoftware/util/SafeSimpleDateFormat.java +++ b/src/main/java/com/cedarsoftware/util/SafeSimpleDateFormat.java @@ -68,7 +68,9 @@ public SafeSimpleDateFormat(String format) dateFormat.setCalendar(cal); dateFormat.setLenient(cal.isLenient()); dateFormat.setTimeZone(cal.getTimeZone()); - dateFormat.setNumberFormat(NumberFormat.getNumberInstance()); + NumberFormat numberFormat = NumberFormat.getNumberInstance(); + numberFormat.setGroupingUsed(false); + dateFormat.setNumberFormat(numberFormat); } public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) diff --git a/src/test/java/com/cedarsoftware/util/TestSimpleDateFormat.java b/src/test/java/com/cedarsoftware/util/TestSimpleDateFormat.java index 092d6949..53c6574c 100644 --- a/src/test/java/com/cedarsoftware/util/TestSimpleDateFormat.java +++ b/src/test/java/com/cedarsoftware/util/TestSimpleDateFormat.java @@ -10,8 +10,12 @@ import java.util.Date; import java.util.Random; import java.util.TimeZone; +import java.util.stream.Stream; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -37,25 +41,35 @@ */ public class TestSimpleDateFormat { - @Test - void testSimpleDateFormat1() throws Exception + @ParameterizedTest + @MethodSource("testDates") + void testSimpleDateFormat1(int year, int month, int day, int hour, int min, int sec, String expectedDateFormat) throws Exception { SafeSimpleDateFormat x = new SafeSimpleDateFormat("yyyy-MM-dd"); - String s = x.format(getDate(2013, 9, 7, 16, 15, 31)); - assertEquals("2013-09-07", s); + String s = x.format(getDate(year, month, day, hour, min, sec)); + assertEquals(expectedDateFormat, s); Date then = x.parse(s); Calendar cal = Calendar.getInstance(); cal.clear(); cal.setTime(then); - assertEquals(2013, cal.get(Calendar.YEAR)); - assertEquals(8, cal.get(Calendar.MONTH)); // Sept - assertEquals(7, cal.get(Calendar.DAY_OF_MONTH)); + assertEquals(year, cal.get(Calendar.YEAR)); + assertEquals(month - 1, cal.get(Calendar.MONTH)); // Sept + assertEquals(day, cal.get(Calendar.DAY_OF_MONTH)); assertEquals(0, cal.get(Calendar.HOUR_OF_DAY)); assertEquals(0, cal.get(Calendar.MINUTE)); assertEquals(0, cal.get(Calendar.SECOND)); } + private static Stream testDates() { + return Stream.of( + Arguments.of(2013, 9, 7, 16, 15, 31, "2013-09-07"), + Arguments.of(169, 5, 1, 11, 45, 15, "0169-05-01"), + Arguments.of(42, 1, 28, 7, 4, 23, "0042-01-28"), + Arguments.of(8, 11, 2, 12, 43, 56, "0008-11-02") + ); + } + @Test void testSetLenient() throws Exception {