Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8299523

LocalDate.parse returns LocalDate for invalid date strings "30.02.2023"

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Java 19, Windows 11

      A DESCRIPTION OF THE PROBLEM :
       LocalDate.parse: Invalid dateStrings for example 2/30/2023 should not return a LocalDate.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      @Test
      void invalidDateStringShouldThrowException() {
      final var formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
      assertThrows(DateTimeParseException.class, () -> LocalDate.parse("30.02.2023", formatter));
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      DateTimeParseException should be thrown
      ACTUAL -
      LocalDate.of(2023,2,28) is returned.

      ---------- BEGIN SOURCE ----------
      @Test
      void invalidDateStringShouldThrowException() {
      final var formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
              // should not return 2/28/2023
      assertThrows(DateTimeParseException.class, () -> LocalDate.parse("30.02.2023", formatter));
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      not use it, write your own method.

            tongwan Andrew Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: