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

Calendar needs some helpful APIs

XMLWordPrintable

      There are two request for this RFE;

      1. Customer asks to add new useful APIs to the Calendar class
      2. Exception is not handled for wrong usage of the time field.
         getMaximum(Calendar.JUNE) should raise exception.

      -----------------------------------------------------------------------
       
      For example: What good is nowing that some month has a max of 31 days and some month has a min of 28 days if I cannot find out which months these are AND determine the number of days for the current month. My proposed method getCurrentMaximum would NOT be a lookup to a static array as the current methods are but use the other field information to determine the answer. For example, the Calendar is set correctly for Jun 10, 1997, now I want to know how many days are in this month, I would simply call getCurrentMaximum(Calendar.DAYS_IN_MONTH) and this call would look at the current month setting and return 30. If I called getCurrentMaximum(Calendar.DAYS_IN_YEAR) it would return 365 since 1997 is not a leap year.

      The fact that this information is not available makes Calendar useless since I still must write a helper class to return these values and then I must create new ones for each Calendar somebody creates and then find a way to use the correct two together. If I create a Calendar bean which has a setCalendar(Calendar x) method then I must check the type of calendar and try to find a matching helper class to return the required values. This greatly impares the localizability of my Calendar bean since I cannot sell this bean to other parts of the world without first knowing what Calendar they are using and write the required helper.

      Thanks

      Murray

      ----------------------------------------------------------------
      >>> Inyoung Cho <###@###.###> 06/05 12:16 AM >>>
      Hi Frank,

      Thanks for the sending the question.
      I found that the Calendar class is poorly documented and it is not catching
      the exceptions if wrong type of "field" is used.
       
      > Problem:
      > There is no way to get the number of days in a month or days in a year or days
      in a month, etc. In the Calendar class, there is two functions, getMaximum(int
      field) and getLeastMaximum(int field) that return for the DAY_IN_MONTH field, 31
      and 28. However what about June which has only 30 days? How is this to be
      determined at runtime?

      getMaximum(int field) and getLeastMaximum(int field) methods take a time field
      as a parameter.

          /**
           * Gets the maximum value for the given time field.
           * e.g. for Gregorian DAY_OF_MONTH, 31.
           * @param field the given time field.
           * @return the maximum value for the given time field.
           */
          abstract public int getMaximum(int field);


          /**
           * Gets the lowest maximum value for the given field if varies.
           * Otherwise same as getMaximum(). e.g., for Gregorian DAY_OF_MONTH, 28.
           * @param field the given time field.
           * @return the lowest maximum value for the given time field.
           */
          abstract public int getLeastMaximum(int field);


      DAY_OF_MONTH field is defined as time field, and JUNE is not defined as time
      field. The excerpt from Calendar.java (found at the end of this mail)
      lists all time fields.

      Therefore, it is illegal to invoke getMaximum(CALENDAR.JUNE).
      This invocation should raise an exception, in my opinion.
      However, the current implementation is returning an invalid value.

      I will create a bug/rfe for this confusing API documentation.

      > Solutions:
      > 1) Add a getCurrentMaximum(int field) that return 30 if the month is June, and
      365 if the year is 1997, etc (Preferred Solution)
      > 2) Require user to keep adding 1 the date until the month value changes. This
      is obviously slow and a great waste of time.

      Your suggestion will be passed down to our design team requesting
      for the additional method APIs for Calendar class.
      For now, your own implementaion is recommended for this functionality.

      Please let me know if you have further question on this.

      Cheers,
      Inyoung

      ------------------ Excerpt from the Calendar.java ---------------------
       
           /**
           * <pre>
           * Field names Minimum Greatest Minimum Least Maximum Maximum
           * ----------- ------- ---------------- ------------- -------
           * ERA 0 0 1 1
           * YEAR 1 1 5,000,000 5,000,000
           * MONTH 0 0 11 11
           * WEEK_OF_YEAR 1 1 53 54
           * WEEK_OF_MONTH 1 1 4 6
           * DAY_OF_MONTH 1 1 28 31
           * DAY_OF_YEAR 1 1 365 366
           * DAY_OF_WEEK 1 1 7 7
           * DAY_OF_WEEK_IN_MONTH -1 -1 4 6
           * AM_PM 0 0 1 1
           * HOUR 0 0 11 12
           * HOUR_OF_DAY 0 0 23 23
           * MINUTE 0 0 59 59
           * SECOND 0 0 59 59
           * MILLISECOND 0 0 999 999
           * ZONE_OFFSET -12*60*60*1000 -12*60*60*1000 12*60*60*1000 12*60*60*1000
           * DST_OFFSET 0 0 1*60*60*1000 1*60*60*1000
           * </pre>
           */
          private static final int MinValues[]
          = {0,1,0,1,1,1,1,1,-1,0,0,0,0,0,0,-12*60*60*1000,0};
          private static final int GreatestMinValues[]
          = {0,1,0,1,1,1,1,1,-1,0,0,0,0,0,0,-12*60*60*1000,0};// same as MinValues
          private static final int LeastMaxValues[]
          = {1,5000000,11,53,4,28,365,7,4,1,11,23,59,59,999,
             12*60*60*1000,1*60*60*1000};
          private static final int MaxValues[]
          = {1,5000000,11,54,6,31,366,7,6,1,12,23,59,59,999,
             12*60*60*1000,1*60*60*1000};

         ---------
         
       
          /**
           * Gets the maximum value for the given time field.
           * e.g. for Gregorian DAY_OF_MONTH, 31.
           * @param field the given time field.
           * @return the maximum value for the given time field.
           */
          abstract public int getMaximum(int field);


          /**
           * Gets the lowest maximum value for the given field if varies.
           * Otherwise same as getMaximum(). e.g., for Gregorian DAY_OF_MONTH, 28.
           * @param field the given time field.
           * @return the lowest maximum value for the given time field.
           */
          abstract public int getLeastMaximum(int field);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

      ***************************************************************************

      Customer (Corel) has one more request.
      Plese see the following email.
      -----------------------------------------------------------

      From ###@###.### Tue Jun 10 02:16:16 1997
      Date: Mon, 09 Jun 1997 21:58:59 -0400
      From: Murray McCulligh <###@###.###>
      To: Inyoung.Cho@Eng
      Subject: Re: JDK 1.1.2 bug in Calendar class.
      Mime-Version: 1.0
      Content-Disposition: inline

      The functions are setTimeInMillis(long) and getTimeInMillis(). These are protected functions that simply bypass the unnecessary Date object. I can see no good reason to make these protected, especially since they would greatly speed up usage of a Calendar.

      The fact that get(int) and set(int, value) are final prevents me from overwritting these calls in my own Calendar class. ie I want to write a Calendar bean, that sends events when its values change. But I cannot do this since set(..) is final.

      Understand?

      Murray

      >>> Inyoung Cho <###@###.###> 06/09 9:49 PM >>>
      Hi,

      Please help me to understand your questions. I am little confused...


      > One more issue with Calendar. Say I want to find what day of the week the
      month starts on. I believe this is what I must do.
      >
      > 1) a Calendar cal is already set with arbitrary date in month.
      > 2) cal.add(Calendar.DATE,cal.get(Calendar.DATE) - 1)
      > 3) cal.setTime(cal.getTime())
      > 4) cal.get(Calendar.DAY_OF_WEEK)
      >
      > My problem involves step 3. Why is this the only way to force a change in
      other fields? This obviously requires two calculations and the creation of a
      Date object that isn't necessary. There are two methods, computeTime() and
      computeFields() that seem to perform these calculations and don't require me to
      create an extra unrequired Date object.
      > Plus, there are two methods setTime(long) and getTime(long) that also avoid
      the unnecessary creation of Dates. Why are these not public?


      Which getTime, and setTime are you referring to?
      I can not find setTime(long) and getTime(long) in Calandar.java.

      >
      > One more question. It seems extremely restrictive to make the get() and set()
      methods final in an abstract class. This completely prevents someone from
      writting either a Calendar wrapper OR a Calendar that immediately updates its
      other fields.

      You can write myOwnCalnedar class with method setLongDate() that will update
      long Date fields in your calendar using method call set().
      But you have to write your won method to convert the DATE to LongDate, etc.







                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     





                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

            aliusunw Alan Liu (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: