001: /*
002: *
003: *
004: * Copyright 1990-2007 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 java.util;
028:
029: /**
030: * The class Date represents a specific instant in time, with millisecond
031: * precision.
032: * <p>
033: * This class has been subset for the J2ME based on the JDK 1.3 Date class.
034: * Many methods and variables have been pruned, and other methods
035: * simplified, in an effort to reduce the size of this class.
036: * <p>
037: * Although the Date class is intended to reflect coordinated universal
038: * time (UTC), it may not do so exactly, depending on the host environment
039: * of the Java Virtual Machine. Nearly all modern operating systems assume
040: * that 1 day = 24x60x60 = 86400 seconds in all cases. In UTC, however,
041: * about once every year or two there is an extra second, called a "leap
042: * second." The leap second is always added as the last second of the
043: * day, and always on December 31 or June 30. For example, the last minute
044: * of the year 1995 was 61 seconds long, thanks to an added leap second.
045: * Most computer clocks are not accurate enough to be able to reflect the
046: * leap-second distinction.
047: *
048: * @see java.util.TimeZone
049: * @see java.util.Calendar
050: * @version CLDC 1.1 03/13/2002 (Based on JDK 1.3)
051: */
052:
053: public class Date {
054:
055: /* If calendar is null, then fastTime indicates the time in millis.
056: * Otherwise, fastTime is ignored, and calendar indicates the time.
057: */
058: private Calendar calendar;
059: private long fastTime;
060:
061: /**
062: * Allocates a <code>Date</code> object and initializes it to
063: * represent the current time specified number of milliseconds since the
064: * standard base time known as "the epoch", namely January 1,
065: * 1970, 00:00:00 GMT.
066: * @see java.lang.System#currentTimeMillis()
067: */
068: public Date() {
069: this (System.currentTimeMillis());
070: }
071:
072: /**
073: * Allocates a <code>Date</code> object and initializes it to
074: * represent the specified number of milliseconds since the
075: * standard base time known as "the epoch", namely January 1,
076: * 1970, 00:00:00 GMT.
077: *
078: * @param date the milliseconds since January 1, 1970, 00:00:00 GMT.
079: * @see java.lang.System#currentTimeMillis()
080: */
081: public Date(long date) {
082: calendar = Calendar.getInstance();
083: if (calendar != null) {
084: calendar.setTimeInMillis(date);
085: }
086: fastTime = date;
087: }
088:
089: /**
090: * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
091: * represented by this <tt>Date</tt> object.
092: *
093: * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
094: * represented by this date.
095: *
096: * @see #setTime
097: */
098: public long getTime() {
099: if (calendar != null) {
100: return calendar.getTimeInMillis();
101: } else {
102: return fastTime;
103: }
104: }
105:
106: /**
107: * Sets this <tt>Date</tt> object to represent a point in time that is
108: * <tt>time</tt> milliseconds after January 1, 1970 00:00:00 GMT.
109: *
110: * @param time the number of milliseconds.
111: *
112: * @see #getTime
113: */
114: public void setTime(long time) {
115: if (calendar != null) {
116: calendar.setTimeInMillis(time);
117: }
118: fastTime = time;
119: }
120:
121: /**
122: * Compares two dates for equality.
123: * The result is <code>true</code> if and only if the argument is
124: * not <code>null</code> and is a <code>Date</code> object that
125: * represents the same point in time, to the millisecond, as this object.
126: * <p>
127: * Thus, two <code>Date</code> objects are equal if and only if the
128: * <code>getTime</code> method returns the same <code>long</code>
129: * value for both.
130: *
131: * @param obj the object to compare with.
132: * @return <code>true</code> if the objects are the same;
133: * <code>false</code> otherwise.
134: * @see java.util.Date#getTime()
135: */
136: public boolean equals(Object obj) {
137: return obj != null && obj instanceof Date
138: && getTime() == ((Date) obj).getTime();
139: }
140:
141: /**
142: * Returns a hash code value for this object. The result is the
143: * exclusive OR of the two halves of the primitive <tt>long</tt>
144: * value returned by the {@link Date#getTime}
145: * method. That is, the hash code is the value of the expression:
146: * <blockquote><pre>
147: * (int)(this.getTime()^(this.getTime() >>> 32))</pre></blockquote>
148: *
149: * @return a hash code value for this object.
150: */
151: public int hashCode() {
152: long ht = getTime();
153: return (int) ht ^ (int) (ht >> 32);
154: }
155:
156: /**
157: * Converts this <code>Date</code> object to a <code>String</code>
158: * of the form:
159: * <blockquote><pre>
160: * dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>
161: * where:<ul>
162: * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed,
163: * Thu, Fri, Sat</tt>).
164: * <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun,
165: * Jul, Aug, Sep, Oct, Nov, Dec</tt>).
166: * <li><tt>dd</tt> is the day of the month (<tt>01</tt> through
167: * <tt>31</tt>), as two decimal digits.
168: * <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through
169: * <tt>23</tt>), as two decimal digits.
170: * <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through
171: * <tt>59</tt>), as two decimal digits.
172: * <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through
173: * <tt>61</tt>, as two decimal digits.
174: * <li><tt>zzz</tt> is the time zone (and may reflect daylight savings
175: * time). If time zone information is not available,
176: * then <tt>zzz</tt> is empty - that is, it consists
177: * of no characters at all.
178: * <li><tt>yyyy</tt> is the year, as four decimal digits.
179: * </ul>
180: *
181: * @return a string representation of this date.
182: * @since CLDC 1.1
183: */
184: public String toString() {
185: return com.sun.cldc.util.j2me.CalendarImpl.toString(calendar);
186: }
187: }
|