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

Adding a month to current date not always working

XMLWordPrintable

    • 1.2beta3
    • x86
    • windows_nt
    • Not verified



      Name: joT67522 Date: 12/02/97


      When the date field is 31, sometimes it adds 31 days instead of adding
      1 month when you use the add method to increment the month.

      Notice the error for January, March, May, August, October.
      Okay for July, December.



      import java.util.*;

      public class CalendarBug{
              public static void main(String args[]){
                      CalendarBug c = new CalendarBug();}

              public CalendarBug(){
                      int[] y = {31,28,31,30,31,30,31,31,30,31,30,31};

                      for (int x=0;x<12;x++) {
                              GregorianCalendar gc = new GregorianCalendar(1997,x,y[x]);
                              System.out.println("Original Date: "+(gc.get(Calendar.MONTH)+1)+"/"+gc.get(Calendar.DATE)+"/"+gc.get(Calendar.YEAR));

                              gc.add(Calendar.MONTH, 1);
                              System.out.println("New Date: "+(gc.get(Calendar.MONTH)+1)+"/"+gc.get(Calendar.DATE)+"/"+gc.get(Calendar.YEAR));

                      }

                      System.exit(0);}
      }


      Original Date: 1/31/1997
      New Date: 3/3/1997
      Original Date: 2/28/1997
      New Date: 3/28/1997
      Original Date: 3/31/1997
      New Date: 5/1/1997
      Original Date: 4/30/1997
      New Date: 5/30/1997
      Original Date: 5/31/1997
      New Date: 7/1/1997
      Original Date: 6/30/1997
      New Date: 7/30/1997
      Original Date: 7/31/1997
      New Date: 8/31/1997
      Original Date: 8/31/1997
      New Date: 10/1/1997
      Original Date: 9/30/1997
      New Date: 10/30/1997
      Original Date: 10/31/1997
      New Date: 12/1/1997
      Original Date: 11/30/1997
      New Date: 12/30/1997
      Original Date: 12/31/1997
      New Date: 1/31/1998

      (Review ID: 21209)
      ======================================================================

            aliusunw Alan Liu (Inactive)
            johsunw Joon Oh (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: