< prev index next >

src/java.base/share/classes/sun/util/calendar/BaseCalendar.java

Print this page




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.util.calendar;
  27 
  28 import java.util.TimeZone;
  29 
  30 /**
  31  * The <code>BaseCalendar</code> provides basic calendar calculation
  32  * functions to support the Julian, Gregorian, and Gregorian-based
  33  * calendar systems.
  34  *
  35  * @author Masayoshi Okutsu
  36  * @since 1.5
  37  */
  38 
  39 public abstract class BaseCalendar extends AbstractCalendar {
  40 
  41     public static final int JANUARY = 1;
  42     public static final int FEBRUARY = 2;
  43     public static final int MARCH = 3;
  44     public static final int APRIL = 4;
  45     public static final int MAY = 5;
  46     public static final int JUNE = 6;
  47     public static final int JULY = 7;
  48     public static final int AUGUST = 8;
  49     public static final int SEPTEMBER = 9;
  50     public static final int OCTOBER = 10;
  51     public static final int NOVEMBER = 11;


 273         Date bdate = (Date) date;
 274         int year = bdate.getNormalizedYear();
 275         long month = bdate.getMonth();
 276         if (month <= 0) {
 277             long xm = 1L - month;
 278             year -= (int)((xm / 12) + 1);
 279             month = 13 - (xm % 12);
 280             bdate.setNormalizedYear(year);
 281             bdate.setMonth((int) month);
 282         } else if (month > DECEMBER) {
 283             year += (int)((month - 1) / 12);
 284             month = ((month - 1)) % 12 + 1;
 285             bdate.setNormalizedYear(year);
 286             bdate.setMonth((int) month);
 287         }
 288     }
 289 
 290     /**
 291      * Returns 366 if the specified date is in a leap year, or 365
 292      * otherwise This method does not perform the normalization with
 293      * the specified <code>CalendarDate</code>. The
 294      * <code>CalendarDate</code> must be normalized to get a correct
 295      * value.
 296      *
 297      * @param a <code>CalendarDate</code>
 298      * @return a year length in days
 299      * @throws ClassCastException if the specified date is not a
 300      * {@link BaseCalendar.Date}
 301      */
 302     public int getYearLength(CalendarDate date) {
 303         return isLeapYear(((Date)date).getNormalizedYear()) ? 366 : 365;
 304     }
 305 
 306     public int getYearLengthInMonths(CalendarDate date) {
 307         return 12;
 308     }
 309 
 310     static final int[] DAYS_IN_MONTH
 311         //  12   1   2   3   4   5   6   7   8   9  10  11  12
 312         = { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 313     static final int[] ACCUMULATED_DAYS_IN_MONTH
 314         //  12/1 1/1 2/1 3/1 4/1 5/1 6/1 7/1 8/1 9/1 10/1 11/1 12/1
 315         = {  -30,  0, 31, 59, 90,120,151,181,212,243, 273, 304, 334};
 316 
 317     static final int[] ACCUMULATED_DAYS_IN_MONTH_LEAP


 395                    + CalendarUtils.floorDivide(prevyear, 4)
 396                    - CalendarUtils.floorDivide(prevyear, 100)
 397                    + CalendarUtils.floorDivide(prevyear, 400)
 398                    + CalendarUtils.floorDivide((367 * month - 362), 12);
 399         }
 400 
 401         if (month > FEBRUARY) {
 402             days -=  isLeapYear(year) ? 1 : 2;
 403         }
 404 
 405         // If it's January 1, update the cache.
 406         if (cache != null && isJan1) {
 407             cache.setCache(year, days, isLeapYear(year) ? 366 : 365);
 408         }
 409 
 410         return days;
 411     }
 412 
 413     /**
 414      * Calculates calendar fields and store them in the specified
 415      * <code>CalendarDate</code>.
 416      */
 417     // should be 'protected'
 418     public void getCalendarDateFromFixedDate(CalendarDate date,
 419                                              long fixedDate) {
 420         Date gdate = (Date) date;
 421         int year;
 422         long jan1;
 423         boolean isLeap;
 424         if (gdate.hit(fixedDate)) {
 425             year = gdate.getCachedYear();
 426             jan1 = gdate.getCachedJan1();
 427             isLeap = isLeapYear(year);
 428         } else {
 429             // Looking up FIXED_DATES[] here didn't improve performance
 430             // much. So we calculate year and jan1. getFixedDate()
 431             // will look up FIXED_DATES[] actually.
 432             year = getGregorianYearFromFixedDate(fixedDate);
 433             jan1 = getFixedDate(year, JANUARY, 1, null);
 434             isLeap = isLeapYear(year);
 435             // Update the cache data




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.util.calendar;
  27 
  28 import java.util.TimeZone;
  29 
  30 /**
  31  * The {@code BaseCalendar} provides basic calendar calculation
  32  * functions to support the Julian, Gregorian, and Gregorian-based
  33  * calendar systems.
  34  *
  35  * @author Masayoshi Okutsu
  36  * @since 1.5
  37  */
  38 
  39 public abstract class BaseCalendar extends AbstractCalendar {
  40 
  41     public static final int JANUARY = 1;
  42     public static final int FEBRUARY = 2;
  43     public static final int MARCH = 3;
  44     public static final int APRIL = 4;
  45     public static final int MAY = 5;
  46     public static final int JUNE = 6;
  47     public static final int JULY = 7;
  48     public static final int AUGUST = 8;
  49     public static final int SEPTEMBER = 9;
  50     public static final int OCTOBER = 10;
  51     public static final int NOVEMBER = 11;


 273         Date bdate = (Date) date;
 274         int year = bdate.getNormalizedYear();
 275         long month = bdate.getMonth();
 276         if (month <= 0) {
 277             long xm = 1L - month;
 278             year -= (int)((xm / 12) + 1);
 279             month = 13 - (xm % 12);
 280             bdate.setNormalizedYear(year);
 281             bdate.setMonth((int) month);
 282         } else if (month > DECEMBER) {
 283             year += (int)((month - 1) / 12);
 284             month = ((month - 1)) % 12 + 1;
 285             bdate.setNormalizedYear(year);
 286             bdate.setMonth((int) month);
 287         }
 288     }
 289 
 290     /**
 291      * Returns 366 if the specified date is in a leap year, or 365
 292      * otherwise This method does not perform the normalization with
 293      * the specified {@code CalendarDate}. The
 294      * {@code CalendarDate} must be normalized to get a correct
 295      * value.
 296      *
 297      * @param date a {@code CalendarDate}
 298      * @return a year length in days
 299      * @throws ClassCastException if the specified date is not a
 300      * {@link BaseCalendar.Date}
 301      */
 302     public int getYearLength(CalendarDate date) {
 303         return isLeapYear(((Date)date).getNormalizedYear()) ? 366 : 365;
 304     }
 305 
 306     public int getYearLengthInMonths(CalendarDate date) {
 307         return 12;
 308     }
 309 
 310     static final int[] DAYS_IN_MONTH
 311         //  12   1   2   3   4   5   6   7   8   9  10  11  12
 312         = { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 313     static final int[] ACCUMULATED_DAYS_IN_MONTH
 314         //  12/1 1/1 2/1 3/1 4/1 5/1 6/1 7/1 8/1 9/1 10/1 11/1 12/1
 315         = {  -30,  0, 31, 59, 90,120,151,181,212,243, 273, 304, 334};
 316 
 317     static final int[] ACCUMULATED_DAYS_IN_MONTH_LEAP


 395                    + CalendarUtils.floorDivide(prevyear, 4)
 396                    - CalendarUtils.floorDivide(prevyear, 100)
 397                    + CalendarUtils.floorDivide(prevyear, 400)
 398                    + CalendarUtils.floorDivide((367 * month - 362), 12);
 399         }
 400 
 401         if (month > FEBRUARY) {
 402             days -=  isLeapYear(year) ? 1 : 2;
 403         }
 404 
 405         // If it's January 1, update the cache.
 406         if (cache != null && isJan1) {
 407             cache.setCache(year, days, isLeapYear(year) ? 366 : 365);
 408         }
 409 
 410         return days;
 411     }
 412 
 413     /**
 414      * Calculates calendar fields and store them in the specified
 415      * {@code CalendarDate}.
 416      */
 417     // should be 'protected'
 418     public void getCalendarDateFromFixedDate(CalendarDate date,
 419                                              long fixedDate) {
 420         Date gdate = (Date) date;
 421         int year;
 422         long jan1;
 423         boolean isLeap;
 424         if (gdate.hit(fixedDate)) {
 425             year = gdate.getCachedYear();
 426             jan1 = gdate.getCachedJan1();
 427             isLeap = isLeapYear(year);
 428         } else {
 429             // Looking up FIXED_DATES[] here didn't improve performance
 430             // much. So we calculate year and jan1. getFixedDate()
 431             // will look up FIXED_DATES[] actually.
 432             year = getGregorianYearFromFixedDate(fixedDate);
 433             jan1 = getFixedDate(year, JANUARY, 1, null);
 434             isLeap = isLeapYear(year);
 435             // Update the cache data


< prev index next >