001: /*
002: * @(#)CalendarDate.java 1.6 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package sun.util.calendar;
028:
029: /**
030: * <code>CalendarDate</code> holds the date fields, such as year and
031: * month, to represent a particular date and time.
032: * <code>CalendarDate</code> is independent from any calendar
033: * systems. Unlike {@link java.util.Calendar Calendar},
034: * <code>CalendarDate</code> has methods only to set or get a field and
035: * any calendar calculation operations must be performed with a
036: * {@link CalendarSystem} implementation.
037: *
038: * @see CalendarSystem
039: * @see Gregorian
040: */
041: public class CalendarDate {
042:
043: public static final int UNKNOWN = Integer.MIN_VALUE;
044:
045: private int year;
046: private int month;
047: private int mday;
048: private int dow = UNKNOWN;
049: private int millis; // milliseconds within the day
050:
051: public CalendarDate() {
052: }
053:
054: public CalendarDate(int year, int month, int date) {
055: this .year = year;
056: this .month = month;
057: this .mday = date;
058: }
059:
060: public void setYear(int year) {
061: this .year = year;
062: }
063:
064: public int getYear() {
065: return year;
066: }
067:
068: public void setMonth(int month) {
069: this .month = month;
070: }
071:
072: public int getMonth() {
073: return month;
074: }
075:
076: public void setDate(int date) {
077: mday = date;
078: }
079:
080: public int getDate() {
081: return mday;
082: }
083:
084: public void setDayOfWeek(int dayOfWeek) {
085: dow = dayOfWeek;
086: }
087:
088: public int getDayOfWeek() {
089: return dow;
090: }
091:
092: public void setTimeOfDay(int time) {
093: millis = time;
094: }
095:
096: public int getTimeOfDay() {
097: return millis;
098: }
099:
100: public boolean equals(Object obj) {
101: if (obj == null || !(obj instanceof CalendarDate)) {
102: return false;
103: }
104: CalendarDate that = (CalendarDate) obj;
105: return (year == that.year && month == that.month
106: && mday == that.mday && millis == that.millis);
107: }
108:
109: public int hashCode() {
110: return year << 20 | month << 16 | mday << 11 | (millis >> 10)
111: & 0x3ff;
112: }
113:
114: public String toString() {
115: int h, m, ms;
116: h = millis / CalendarSystem.ONE_HOUR;
117: ms = millis % CalendarSystem.ONE_HOUR;
118: m = ms / CalendarSystem.ONE_MINUTE;
119: ms %= CalendarSystem.ONE_MINUTE;
120:
121: String ss, mss;
122: if (ms == 0) {
123: ss = "";
124: mss = "";
125: } else {
126: ss = ":" + sprintf02d(ms / CalendarSystem.ONE_SECOND);
127: ms %= CalendarSystem.ONE_SECOND;
128: if (ms == 0) {
129: mss = "";
130: } else {
131: mss = ".";
132: if (ms < 100) {
133: mss += "0";
134: }
135: mss += sprintf02d(ms);
136: }
137: }
138:
139: return year + "/" + sprintf02d(month + 1) + "/"
140: + sprintf02d(mday) + " " + sprintf02d(h) + ":"
141: + sprintf02d(m) + ss + mss;
142: }
143:
144: private static final String sprintf02d(int d) {
145: StringBuffer sb = new StringBuffer();
146: if (d < 10) {
147: sb.append('0');
148: }
149: sb.append(Integer.toString(d));
150: return sb.toString();
151: }
152: }
|