Skip to content

ISO8061Utils.parse() accepts non-existent dates #2133

Description

@JPedroAmorim

Gson version

2.9.0

Java / Android version

java 16 2021-03-16
Java(TM) SE Runtime Environment (build 16+36-2231)
Java HotSpot(TM) 64-Bit Server VM (build 16+36-2231, mixed mode, sharing)

Description

Apparently ISO8061Utils.parse() works in a very lenient manner when dealing with dates that do not exist (for instance 2022-14-30), generating valid Date objects.

Given this unit test:

 @Test
    public void testDateParseNonExistentDate() throws ParseException {
        String dateStr = "2022-14-30";
        try {
            Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
            fail("Should've thrown exception");
        } catch (Exception expected) {

        }
    }

It fails and produces a Date object whose toString() yields:

Thu Mar 02 00:00:00 BRT 2023

This also applies for instances where the day is invalid as well.

 @Test
    public void testDateParseNonExistentDate() throws ParseException {
        String dateStr = "2022-12-33";
        try {
            Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0));
            fail("Should've thrown exception");
        } catch (Exception expected) {

        }
    }

It fails and produces a Date object whose toString() yields:

Mon Jan 02 00:00:00 BRT 2023

Expected behavior

An exception to be thrown, likely IllegalArgumentException.

Actual behavior

A valid Date object was generated that "absorbed" the surplus from the illegal argument.

Note

If this is expected behavior, let me know and I'll close the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions