0N/A/*
2362N/A * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.util.calendar;
0N/A
0N/Aimport java.lang.Cloneable;
0N/Aimport java.util.Locale;
0N/Aimport java.util.TimeZone;
0N/A
0N/A/**
0N/A * The <code>CalendarDate</code> class represents a specific instant
0N/A * in time by calendar date and time fields that are multiple cycles
0N/A * in different time unites. The semantics of each calendar field is
0N/A * given by a concrete calendar system rather than this
0N/A * <code>CalendarDate</code> class that holds calendar field values
0N/A * without interpreting them. Therefore, this class can be used to
0N/A * represent an amount of time, such as 2 years and 3 months.
0N/A *
0N/A * <p>A <code>CalendarDate</code> instance can be created by calling
0N/A * the <code>newCalendarDate</code> or <code>getCalendarDate</code>
0N/A * methods in <code>CalendarSystem</code>. A
0N/A * <code>CalendarSystem</code> instance is obtained by calling one of
0N/A * the factory methods in <code>CalendarSystem</code>. Manipulations
0N/A * of calendar dates must be handled by the calendar system by which
0N/A * <code>CalendarDate</code> instances have been created.
0N/A *
0N/A * <p>Some calendar fields can be modified through method calls. Any
0N/A * modification of a calendar field brings the state of a
0N/A * <code>CalendarDate</code> to <I>not normalized</I>. The
0N/A * normalization must be performed to make all the calendar fields
0N/A * consistent with a calendar system.
0N/A *
0N/A * <p>The <code>protected</code> methods are intended to be used for
0N/A * implementing a concrete calendar system, not for general use as an
0N/A * API.
0N/A *
0N/A * @see CalendarSystem
0N/A * @author Masayoshi Okutsu
0N/A * @since 1.5
0N/A */
0N/Apublic abstract class CalendarDate implements Cloneable {
0N/A public static final int FIELD_UNDEFINED = Integer.MIN_VALUE;
0N/A public static final long TIME_UNDEFINED = Long.MIN_VALUE;
0N/A
0N/A private Era era;
0N/A private int year;
0N/A private int month;
0N/A private int dayOfMonth;
0N/A private int dayOfWeek = FIELD_UNDEFINED;
0N/A private boolean leapYear;
0N/A
0N/A private int hours;
0N/A private int minutes;
0N/A private int seconds;
0N/A private int millis; // fractional part of the second
0N/A private long fraction; // time of day value in millisecond
0N/A
0N/A private boolean normalized;
0N/A
0N/A private TimeZone zoneinfo;
0N/A private int zoneOffset;
0N/A private int daylightSaving;
0N/A private boolean forceStandardTime;
0N/A
0N/A private Locale locale;
0N/A
0N/A protected CalendarDate() {
0N/A this(TimeZone.getDefault());
0N/A }
0N/A
0N/A protected CalendarDate(TimeZone zone) {
0N/A zoneinfo = zone;
0N/A }
0N/A
0N/A public Era getEra() {
0N/A return era;
0N/A }
0N/A
0N/A /**
0N/A * Sets the era of the date to the specified era. The default
0N/A * implementation of this method accepts any Era value, including
0N/A * <code>null</code>.
0N/A *
0N/A * @exception NullPointerException if the calendar system for this
0N/A * <code>CalendarDate</code> requires eras and the specified era
0N/A * is null.
0N/A * @exception IllegalArgumentException if the specified
0N/A * <code>era</code> is unknown to the calendar
0N/A * system for this <code>CalendarDate</code>.
0N/A */
0N/A public CalendarDate setEra(Era era) {
0N/A if (this.era == era) {
0N/A return this;
0N/A }
0N/A this.era = era;
0N/A normalized = false;
0N/A return this;
0N/A }
0N/A
0N/A public int getYear() {
0N/A return year;
0N/A }
0N/A
0N/A public CalendarDate setYear(int year) {
0N/A if (this.year != year) {
0N/A this.year = year;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public CalendarDate addYear(int n) {
0N/A if (n != 0) {
0N/A year += n;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether the year represented by this
0N/A * <code>CalendarDate</code> is a leap year. If leap years are
0N/A * not applicable to the calendar system, this method always
0N/A * returns <code>false</code>.
0N/A *
0N/A * <p>If this <code>CalendarDate</code> hasn't been normalized,
0N/A * <code>false</code> is returned. The normalization must be
0N/A * performed to retrieve the correct leap year information.
0N/A *
0N/A * @return <code>true</code> if this <code>CalendarDate</code> is
0N/A * normalized and the year of this <code>CalendarDate</code> is a
0N/A * leap year, or <code>false</code> otherwise.
0N/A * @see BaseCalendar#isGregorianLeapYear
0N/A */
0N/A public boolean isLeapYear() {
0N/A return leapYear;
0N/A }
0N/A
0N/A void setLeapYear(boolean leapYear) {
0N/A this.leapYear = leapYear;
0N/A }
0N/A
0N/A public int getMonth() {
0N/A return month;
0N/A }
0N/A
0N/A public CalendarDate setMonth(int month) {
0N/A if (this.month != month) {
0N/A this.month = month;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public CalendarDate addMonth(int n) {
0N/A if (n != 0) {
0N/A month += n;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public int getDayOfMonth() {
0N/A return dayOfMonth;
0N/A }
0N/A
0N/A public CalendarDate setDayOfMonth(int date) {
0N/A if (dayOfMonth != date) {
0N/A dayOfMonth = date;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public CalendarDate addDayOfMonth(int n) {
0N/A if (n != 0) {
0N/A dayOfMonth += n;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Returns the day of week value. If this CalendarDate is not
0N/A * normalized, {@link #FIELD_UNDEFINED} is returned.
0N/A *
0N/A * @return day of week or {@link #FIELD_UNDEFINED}
0N/A */
0N/A public int getDayOfWeek() {
0N/A if (!isNormalized()) {
0N/A dayOfWeek = FIELD_UNDEFINED;
0N/A }
0N/A return dayOfWeek;
0N/A }
0N/A
0N/A public int getHours() {
0N/A return hours;
0N/A }
0N/A
0N/A public CalendarDate setHours(int hours) {
0N/A if (this.hours != hours) {
0N/A this.hours = hours;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public CalendarDate addHours(int n) {
0N/A if (n != 0) {
0N/A hours += n;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public int getMinutes() {
0N/A return minutes;
0N/A }
0N/A
0N/A public CalendarDate setMinutes(int minutes) {
0N/A if (this.minutes != minutes) {
0N/A this.minutes = minutes;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public CalendarDate addMinutes(int n) {
0N/A if (n != 0) {
0N/A minutes += n;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public int getSeconds() {
0N/A return seconds;
0N/A }
0N/A
0N/A public CalendarDate setSeconds(int seconds) {
0N/A if (this.seconds != seconds) {
0N/A this.seconds = seconds;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public CalendarDate addSeconds(int n) {
0N/A if (n != 0) {
0N/A seconds += n;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public int getMillis() {
0N/A return millis;
0N/A }
0N/A
0N/A public CalendarDate setMillis(int millis) {
0N/A if (this.millis != millis) {
0N/A this.millis = millis;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public CalendarDate addMillis(int n) {
0N/A if (n != 0) {
0N/A millis += n;
0N/A normalized = false;
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A public long getTimeOfDay() {
0N/A if (!isNormalized()) {
0N/A return fraction = TIME_UNDEFINED;
0N/A }
0N/A return fraction;
0N/A }
0N/A
0N/A public CalendarDate setDate(int year, int month, int dayOfMonth) {
0N/A setYear(year);
0N/A setMonth(month);
0N/A setDayOfMonth(dayOfMonth);
0N/A return this;
0N/A }
0N/A
0N/A public CalendarDate addDate(int year, int month, int dayOfMonth) {
0N/A addYear(year);
0N/A addMonth(month);
0N/A addDayOfMonth(dayOfMonth);
0N/A return this;
0N/A }
0N/A
0N/A public CalendarDate setTimeOfDay(int hours, int minutes, int seconds, int millis) {
0N/A setHours(hours);
0N/A setMinutes(minutes);
0N/A setSeconds(seconds);
0N/A setMillis(millis);
0N/A return this;
0N/A }
0N/A
0N/A public CalendarDate addTimeOfDay(int hours, int minutes, int seconds, int millis) {
0N/A addHours(hours);
0N/A addMinutes(minutes);
0N/A addSeconds(seconds);
0N/A addMillis(millis);
0N/A return this;
0N/A }
0N/A
0N/A protected void setTimeOfDay(long fraction) {
0N/A this.fraction = fraction;
0N/A }
0N/A
0N/A public boolean isNormalized() {
0N/A return normalized;
0N/A }
0N/A
0N/A
0N/A public boolean isStandardTime() {
0N/A return forceStandardTime;
0N/A }
0N/A
0N/A public void setStandardTime(boolean standardTime) {
0N/A forceStandardTime = standardTime;
0N/A }
0N/A
0N/A public boolean isDaylightTime() {
0N/A if (isStandardTime()) {
0N/A return false;
0N/A }
0N/A return daylightSaving != 0;
0N/A }
0N/A
0N/A protected void setLocale(Locale loc) {
0N/A locale = loc;
0N/A }
0N/A
0N/A public TimeZone getZone() {
0N/A return zoneinfo;
0N/A }
0N/A
0N/A public CalendarDate setZone(TimeZone zoneinfo) {
0N/A this.zoneinfo = zoneinfo;
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether the specified date is the same date of this
0N/A * <code>CalendarDate</code>. The time of the day fields are
0N/A * ignored for the comparison.
0N/A */
0N/A public boolean isSameDate(CalendarDate date) {
0N/A return getDayOfWeek() == date.getDayOfWeek()
0N/A && getMonth() == date.getMonth()
0N/A && getYear() == date.getYear()
0N/A && getEra() == date.getEra();
0N/A }
0N/A
0N/A public boolean equals(Object obj) {
0N/A if (!(obj instanceof CalendarDate)) {
0N/A return false;
0N/A }
0N/A CalendarDate that = (CalendarDate) obj;
0N/A if (isNormalized() != that.isNormalized()) {
0N/A return false;
0N/A }
0N/A boolean hasZone = zoneinfo != null;
0N/A boolean thatHasZone = that.zoneinfo != null;
0N/A if (hasZone != thatHasZone) {
0N/A return false;
0N/A }
0N/A if (hasZone && !zoneinfo.equals(that.zoneinfo)) {
0N/A return false;
0N/A }
0N/A return (getEra() == that.getEra()
0N/A && year == that.year
0N/A && month == that.month
0N/A && dayOfMonth == that.dayOfMonth
0N/A && hours == that.hours
0N/A && minutes == that.minutes
0N/A && seconds == that.seconds
0N/A && millis == that.millis
0N/A && zoneOffset == that.zoneOffset);
0N/A }
0N/A
0N/A public int hashCode() {
0N/A // a pseudo (local standard) time stamp value in milliseconds
0N/A // from the Epoch, assuming Gregorian calendar fields.
0N/A long hash = ((((((long)year - 1970) * 12) + (month - 1)) * 30) + dayOfMonth) * 24;
0N/A hash = ((((((hash + hours) * 60) + minutes) * 60) + seconds) * 1000) + millis;
0N/A hash -= zoneOffset;
0N/A int normalized = isNormalized() ? 1 : 0;
0N/A int era = 0;
0N/A Era e = getEra();
0N/A if (e != null) {
0N/A era = e.hashCode();
0N/A }
0N/A int zone = zoneinfo != null ? zoneinfo.hashCode() : 0;
0N/A return (int) hash * (int)(hash >> 32) ^ era ^ normalized ^ zone;
0N/A }
0N/A
0N/A /**
0N/A * Returns a copy of this <code>CalendarDate</code>. The
0N/A * <code>TimeZone</code> object, if any, is not cloned.
0N/A *
0N/A * @return a copy of this <code>CalendarDate</code>
0N/A */
0N/A public Object clone() {
0N/A try {
0N/A return super.clone();
0N/A } catch (CloneNotSupportedException e) {
0N/A // this shouldn't happen
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Converts calendar date values to a <code>String</code> in the
0N/A * following format.
0N/A * <pre>
0N/A * yyyy-MM-dd'T'HH:mm:ss.SSSz
0N/A * </pre>
0N/A *
0N/A * @see java.text.SimpleDateFormat
0N/A */
0N/A public String toString() {
0N/A StringBuilder sb = new StringBuilder();
0N/A CalendarUtils.sprintf0d(sb, year, 4).append('-');
0N/A CalendarUtils.sprintf0d(sb, month, 2).append('-');
0N/A CalendarUtils.sprintf0d(sb, dayOfMonth, 2).append('T');
0N/A CalendarUtils.sprintf0d(sb, hours, 2).append(':');
0N/A CalendarUtils.sprintf0d(sb, minutes, 2).append(':');
0N/A CalendarUtils.sprintf0d(sb, seconds, 2).append('.');
0N/A CalendarUtils.sprintf0d(sb, millis, 3);
0N/A if (zoneOffset == 0) {
0N/A sb.append('Z');
0N/A } else if (zoneOffset != FIELD_UNDEFINED) {
0N/A int offset;
0N/A char sign;
0N/A if (zoneOffset > 0) {
0N/A offset = zoneOffset;
0N/A sign = '+';
0N/A } else {
0N/A offset = -zoneOffset;
0N/A sign = '-';
0N/A }
0N/A offset /= 60000;
0N/A sb.append(sign);
0N/A CalendarUtils.sprintf0d(sb, offset / 60, 2);
0N/A CalendarUtils.sprintf0d(sb, offset % 60, 2);
0N/A } else {
0N/A sb.append(" local time");
0N/A }
0N/A return sb.toString();
0N/A }
0N/A
0N/A protected void setDayOfWeek(int dayOfWeek) {
0N/A this.dayOfWeek = dayOfWeek;
0N/A }
0N/A
0N/A protected void setNormalized(boolean normalized) {
0N/A this.normalized = normalized;
0N/A }
0N/A
0N/A public int getZoneOffset() {
0N/A return zoneOffset;
0N/A }
0N/A
0N/A protected void setZoneOffset(int offset) {
0N/A zoneOffset = offset;
0N/A }
0N/A
0N/A public int getDaylightSaving() {
0N/A return daylightSaving;
0N/A }
0N/A
0N/A protected void setDaylightSaving(int daylightSaving) {
0N/A this.daylightSaving = daylightSaving;
0N/A }
0N/A}