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

java.time.LocalDate does not support JUNE 31.

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      Java version "1.8.0_144"
      Java(TM) SE Runtime Enviroment (build 1.8.0_144-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 64bit Enterprise Edition

      A DESCRIPTION OF THE PROBLEM :
      LocalDate.of() (or create) not support JUNE 31.
      If you use this code at JUNE 31,you will be get error.

      public static LocalDateTime of(Calendar calc){
              return LocalDateTime.of(calc.get((Calendar.YEAR)),calc.get(Calendar.MONTH),calc.get(Calendar.DATE),calc.get(Calendar.HOUR_OF_DAY)
              ,calc.get(Calendar.MINUTE),calc.get(Calendar.SECOND));
          }

      this is error printstrack.


      Exception in thread "main" java.time.DateTimeException: Invalid date 'JUNE 31'
      at java.time.LocalDate.create(LocalDate.java:431)
      at java.time.LocalDate.of(LocalDate.java:269)
      at java.time.LocalDateTime.of(LocalDateTime.java:336)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Fix create() code from this to next.

      Original code:
      private static LocalDate create(int year, int month, int dayOfMonth) {
              if (dayOfMonth > 28) {
                  int dom = 31;
                  switch (month) {
                      case 2:
                          dom = (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
                          break;
                      case 4:
                      case 6:
                      case 9:
                      case 11:
                          dom = 30;
                          break;
                  }
                  if (dayOfMonth > dom) {
                      if (dayOfMonth == 29) {
                          throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year");
                      } else {
                          throw new DateTimeException("Invalid date '" + Month.of(month).name() + " " + dayOfMonth + "'");
                      }
                  }
              }
              return new LocalDate(year, month, dayOfMonth);
          }



      Fixed code:

      private static LocalDate create(int year, int month, int dayOfMonth) {
              if (dayOfMonth > 28) {
                  int dom = 31;
                  switch (month) {
                      case 2:
                          dom = (IsoChronology.INSTANCE.isLeapYear(year) ? 29 : 28);
                          break;
                      case 4:
                      case 9:
                      case 11:
                          dom = 30;
                          break;
                  }
                  if (dayOfMonth > dom) {
                      if (dayOfMonth == 29) {
                          throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year");
                      } else {
                          throw new DateTimeException("Invalid date '" + Month.of(month).name() + " " + dayOfMonth + "'");
                      }
                  }
              }
              return new LocalDate(year, month, dayOfMonth);
          }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Bug will be fixed.
      ACTUAL -
      Printstrack like this.


      Exception in thread "main" java.time.DateTimeException: Invalid date 'JUNE 31'
      at java.time.LocalDate.create(LocalDate.java:431)
      at java.time.LocalDate.of(LocalDate.java:269)
      at java.time.LocalDateTime.of(LocalDateTime.java:336)

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.time.DateTimeException: Invalid date 'JUNE 31'
      at java.time.LocalDate.create(LocalDate.java:431)
      at java.time.LocalDate.of(LocalDate.java:269)
      at java.time.LocalDateTime.of(LocalDateTime.java:336)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      SUPPORT :
      YES

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: