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

The Calendar class when rolling the HOUR field changes the date improperly.

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      1.1.6
    • CPU:
      x86
    • OS:
      windows_95
    • Verification:
      Verified

      Backports

        Description



        Name: rlT66838 Date: 08/14/97


        /*
        The following appears to be a defect in the Calendar class roll method:

        The roll method of the calendar class appears to be broken for
        rolling of hours. The sample applet provided creates two text
        entry fields where a formatted date is printed. The buttons
        up2 and down2 will roll the hour field up or down respectively.
        When the time changes from 10PM to 11PM the day field will change
        from Aug-6 to Aug-5. This makes no sense whatsoever.
        Besides when you roll an hour of a day, the date should never
        change.

        The remainder of this comment are enhancements requests
        for the calendar class:

        The first entry field in the applet was written to roll the month. I noticed
        that the roll function was automatically adjusting for daylights
        savings time, in April and October. It would be nice if this
        automatical adjustment could be disabled. This is especially useful
        if you are dealing with time from an absolute standpoint of, on
        this date and time.

        The rolling behavior for Months states that when rolling a month like
        Jan 31 upwards will give a date of Mar 3, since February only has
        28 days (unless it's a leap year). Instead of adjusting both the day
        and month into the next month skipping February when you roll the
        month forward wouldn't it make more sense to just adjust the day back
        to 28(or 29) and only roll the month forward only one month?

        Both of these changes are necessary if the developer is working
        with the Calendar class to create a text entry field which will
        scroll through the date and time.
        */




        import java.awt.*;
        import java.applet.*;
        import java.text.*;
        import java.util.*;
        import java.awt.event.*;

        public class Applet1 extends Applet
        implements ActionListener
        {
            Panel borderpanel;
            Button b1, b2, b3, b4;
            DateSpinner ds1, ds2;

            public void init() {
        // I am using a specific date here since that is the day
        // on which I saw the defect. If you wish to use the
        // current date, comment out the following two lines
        // and uncomment the third line.
        long l = 870915849162L;
        Date d = new Date(l);
        // Date d = new Date();
        setSize(270, 140);
        b1 = new Button("up1");
        b2 = new Button("down1");
                ds1 = new DateSpinner(d);
        b3 = new Button("up2");
        b4 = new Button("down2");
        ds2 = new DateSpinner(d);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
          add(b1);
          add(b2);
          add(ds1);
          add(b3);
          add(b4);
          add(ds2);
            }

            public void actionPerformed(ActionEvent event) {
        // System.out.println("Action event: " + event);

        if (event.getActionCommand().equals ("up1"))
          ds1.doRoll(DateSpinner.MON, true);
        if (event.getActionCommand().equals ("down1"))
          ds1.doRoll(DateSpinner.MON, false);

        if (event.getActionCommand().equals ("up2"))
          ds2.doRoll(DateSpinner.HOUR, true);
        if (event.getActionCommand().equals ("down2"))
          ds2.doRoll(DateSpinner.HOUR, false);

        // ds1.doRoll(DateSpinner.DAY, true);
        // ds1.doRoll(DateSpinner.MON, true);
        // ds1.doRoll(DateSpinner.YEAR, true);
        // ds1.doRoll(DateSpinner.HOUR, true);
        // ds1.doRoll(DateSpinner.MIN, true);
        // ds1.doRoll(DateSpinner.SEC, true);
            }

        }


        class DateSpinner extends TextField {
            static final int MON = 1;
            static final int DAY = 2;
            static final int YEAR = 3;
            static final int HOUR = 4;
            static final int MIN = 5;
            static final int SEC = 6;
           
            private Date date;
            private DateFormat df;
            private Calendar mycal;
            private String entry;

            public DateSpinner() {
        date = new Date();
        mycal = Calendar.getInstance();
        mycal.setTime(date);
        df = DateFormat.getDateTimeInstance();
        entry = df.format(date);
        setText(entry);
            }

            public DateSpinner(Date d) {
        date = d;
        mycal = Calendar.getInstance();
        mycal.setTime(date);
        df = DateFormat.getDateTimeInstance();
        entry = df.format(date);
        setText(entry);
            }

            public void doRoll(int itype, boolean up) {
        switch (itype) {
        case MON:
        mycal.roll(Calendar.MONTH, up);
        break;
        case DAY:
        mycal.roll(Calendar.DAY_OF_MONTH, up);
        break;
        case YEAR:
        mycal.roll(Calendar.YEAR, up);
        break;
        case HOUR:
        mycal.roll(Calendar.HOUR_OF_DAY, up);
        break;
        case MIN:
        mycal.roll(Calendar.MINUTE, up);
        break;
        case SEC:
        mycal.roll(Calendar.SECOND, up);
        break;
        default:
        System.out.println("Unable to modify field type: "+itype);
        break;
        }
        date = mycal.getTime();
        entry = df.format(date);
        setText(entry);
            }
        }

        company - Hewlett-Packard , email - ###@###.###
        ======================================================================

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                joconnersunw John Oconner (Inactive)
                Reporter:
                rlewis Roger Lewis (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: