Skip to content

Commit

Permalink
Fixed to properly format dates having years with fewer than four digits.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kittrell committed Feb 16, 2024
1 parent 55eccb0 commit 7dda350
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 21 additions & 7 deletions src/test/java/com/cedarsoftware/util/TestSimpleDateFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Arguments> 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
{
Expand Down

0 comments on commit 7dda350

Please sign in to comment.