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

GregorianCalendar.add (Calendar.MONTH) does not work for BC dates

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P5 P5
    • 1.4.0
    • 6.1.2, 1.2.2, 6
    • core-libs
    • beta
    • generic, sparc
    • generic, solaris_2.6



      Name: yyT116575 Date: 09/20/2000


      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)

      If one has a Gregorian Calendar object set to a BC date, and one calls the add
      () method with a field of Calendar.MONTH, the years will update backwards
      (i.e., it incorrectly gives the month after December of 5 BC as January of 6 BC
      when it should give January of 4 BC.) Below I have some code to demonstrate
      the bug; it creates a new GregorianCalendar, sets the date back to January 1st
      of 4 BC, and then loops through and increments the MONTH field by one using the
      add method.


      import java.util.*;

      public class Greg {

      public static void main (String [] args) {
      GregorianCalendar eek = new GregorianCalendar (1, 1, 1);
      eek.add (eek.YEAR, -5);
      for (int i = 0; i < 40; i++) {
      eek.add (eek.MONTH, 1);
      System.out.println (eek.get (eek.YEAR) +
      " " + eek.get (eek.MONTH) + " " +
      (eek.get (eek.ERA) ==
      eek.BC ? "BC" : "AD"));
      }
      }

      }


      Running this in JDK 1.2.2 produces the following output:


      5 2 BC
      5 3 BC
      5 4 BC
      5 5 BC
      5 6 BC
      5 7 BC
      5 8 BC
      5 9 BC
      5 10 BC
      5 11 BC
      6 0 BC
      6 1 BC
      6 2 BC
      6 3 BC
      6 4 BC
      6 5 BC
      6 6 BC
      6 7 BC
      6 8 BC
      6 9 BC
      6 10 BC
      6 11 BC
      7 0 BC
      7 1 BC
      7 2 BC
      7 3 BC
      7 4 BC
      7 5 BC
      7 6 BC
      7 7 BC
      7 8 BC
      7 9 BC
      7 10 BC
      7 11 BC
      8 0 BC
      8 1 BC
      8 2 BC
      8 3 BC
      8 4 BC
      8 5 BC


      As one can see from the above output, every time the month should wrap around
      from December to January (11 to 0), the year increases rather than decreases,
      whereas BC dates should decrease when time goes forward.
      (Review ID: 108518)
      ======================================================================

            peytoia Yuka Kamiya (Inactive)
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: