001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package java.util;
017:
018: import java.io.Serializable;
019:
020: /**
021: * Represents a date and time.
022: */
023: public class Date implements Cloneable, Comparable<Date>, Serializable {
024: /**
025: * Used only by toString().
026: */
027: private static final String[] DAYS = { "Sun", "Mon", "Tue", "Wed",
028: "Thu", "Fri", "Sat" };
029:
030: /**
031: * Used only by toString().
032: */
033: private static final String[] MONTHS = { "Jan", "Feb", "Mar",
034: "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
035: "Dec" };
036:
037: // CHECKSTYLE_OFF: The underscore prefix is an old convention that could be
038: // easily replaced.
039: public static native long __parse(String s) /*-{
040: var d = Date.parse(s);
041: return isNaN(d) ? -1 : d;
042: }-*/;
043:
044: public static long parse(String s) {
045: long d = __parse(s);
046: if (d != -1) {
047: return d;
048: } else {
049: throw new IllegalArgumentException();
050: }
051: }
052:
053: // CHECKSTYLE_OFF: Matching the spec.
054: public static native long UTC(int year, int month, int date,
055: int hrs, int min, int sec) /*-{
056: return Date.UTC(year + 1900, month, date, hrs, min, sec);
057: }-*/;
058:
059: /**
060: * Return the names for the days of the week as specified by the Date
061: * specification.
062: */
063: @SuppressWarnings("unused")
064: // called by JSNI
065: private static String dayToString(int day) {
066: return DAYS[day];
067: }
068:
069: /**
070: * Return the names for the months of the year as specified by the Date
071: * specification.
072: */
073: @SuppressWarnings("unused")
074: // called by JSNI
075: private static String monthToString(int month) {
076: return MONTHS[month];
077: }
078:
079: /**
080: * Ensure a number is displayed with two digits.
081: * @return A two-character representation of the number.
082: */
083: @SuppressWarnings("unused")
084: // called by JSNI
085: private static String padTwo(int number) {
086: if (number < 10) {
087: return "0" + number;
088: } else {
089: return String.valueOf(number);
090: }
091: }
092:
093: public Date() {
094: init();
095: }
096:
097: public Date(int year, int month, int date) {
098: init(year, month, date, 0, 0, 0);
099: }
100:
101: public Date(int year, int month, int date, int hrs, int min) {
102: init(year, month, date, hrs, min, 0);
103: }
104:
105: public Date(int year, int month, int date, int hrs, int min, int sec) {
106: init(year, month, date, hrs, min, sec);
107: }
108:
109: public Date(long date) {
110: init(date);
111: }
112:
113: public Date(String date) {
114: init(Date.parse(date));
115: }
116:
117: public boolean after(Date when) {
118: return getTime() > when.getTime();
119: }
120:
121: public boolean before(Date when) {
122: return getTime() < when.getTime();
123: }
124:
125: public Object clone() {
126: return new Date(getTime());
127: }
128:
129: public int compareTo(Date other) {
130: long this Time = getTime();
131: long otherTime = other.getTime();
132: if (this Time < otherTime) {
133: return -1;
134: } else if (this Time > otherTime) {
135: return 1;
136: } else {
137: return 0;
138: }
139: }
140:
141: @Override
142: public boolean equals(Object obj) {
143: return ((obj instanceof Date) && (getTime() == ((Date) obj)
144: .getTime()));
145: }
146:
147: public native int getDate() /*-{
148: return this.jsdate.getDate();
149: }-*/;
150:
151: public native int getDay() /*-{
152: return this.jsdate.getDay();
153: }-*/;
154:
155: public native int getHours() /*-{
156: return this.jsdate.getHours();
157: }-*/;
158:
159: public native int getMinutes() /*-{
160: return this.jsdate.getMinutes();
161: }-*/;
162:
163: public native int getMonth() /*-{
164: return this.jsdate.getMonth();
165: }-*/;
166:
167: public native int getSeconds() /*-{
168: return this.jsdate.getSeconds();
169: }-*/;
170:
171: public native long getTime() /*-{
172: return this.jsdate.getTime();
173: }-*/;
174:
175: public native int getTimezoneOffset() /*-{
176: return this.jsdate.getTimezoneOffset();
177: }-*/;
178:
179: public native int getYear() /*-{
180: return this.jsdate.getFullYear()-1900;
181: }-*/;
182:
183: @Override
184: public int hashCode() {
185: return (int) (this .getTime() ^ (this .getTime() >>> 32));
186: }
187:
188: public native void setDate(int date) /*-{
189: this.jsdate.setDate(date);
190: }-*/;
191:
192: public native void setHours(int hours) /*-{
193: this.jsdate.setHours(hours);
194: }-*/;
195:
196: public native void setMinutes(int minutes) /*-{
197: this.jsdate.setMinutes(minutes);
198: }-*/;
199:
200: public native void setMonth(int month) /*-{
201: this.jsdate.setMonth(month);
202: }-*/;
203:
204: // CHECKSTYLE_ON
205:
206: public native void setSeconds(int seconds) /*-{
207: this.jsdate.setSeconds(seconds);
208: }-*/;;
209:
210: public native void setTime(long time) /*-{
211: this.jsdate.setTime(time);
212: }-*/;
213:
214: // CHECKSTYLE_ON
215:
216: public native void setYear(int year) /*-{
217: this.jsdate.setFullYear(year + 1900);
218: }-*/;
219:
220: public native String toGMTString() /*-{
221: var d = this.jsdate;
222: var padTwo = @java.util.Date::padTwo(I);
223: var month =
224: @java.util.Date::monthToString(I)(this.jsdate.getUTCMonth());
225:
226: return d.getUTCDate() + " " +
227: month + " " +
228: d.getUTCFullYear() + " " +
229: padTwo(d.getUTCHours()) + ":" +
230: padTwo(d.getUTCMinutes()) + ":" +
231: padTwo(d.getUTCSeconds()) +
232: " GMT";
233: }-*/;
234:
235: public native String toLocaleString() /*-{
236: return this.jsdate.toLocaleString();
237: }-*/;
238:
239: @Override
240: public native String toString() /*-{
241: var d = this.jsdate;
242: var padTwo = @java.util.Date::padTwo(I);
243: var day =
244: @java.util.Date::dayToString(I)(this.jsdate.getDay());
245: var month =
246: @java.util.Date::monthToString(I)(this.jsdate.getMonth());
247:
248: // Compute timezone offset. The value that getTimezoneOffset returns is
249: // backwards for the transformation that we want.
250: var offset = -d.getTimezoneOffset();
251: var hourOffset = String((offset >= 0) ?
252: "+" + Math.floor(offset / 60) : Math.ceil(offset / 60));
253: var minuteOffset = padTwo(Math.abs(offset) % 60);
254:
255: return day + " " + month + " " +
256: padTwo(d.getDate()) + " " +
257: padTwo(d.getHours()) + ":" +
258: padTwo(d.getMinutes()) + ":" +
259: padTwo(d.getSeconds()) +
260: " GMT" + hourOffset + minuteOffset +
261: + " " + d.getFullYear();
262: }-*/;
263:
264: private native void init() /*-{
265: this.jsdate = new Date();
266: }-*/;
267:
268: private native void init(int year, int month, int date, int hrs,
269: int min, int sec) /*-{
270: this.jsdate = new Date();
271: this.jsdate.setFullYear(year + 1900, month, date);
272: this.jsdate.setHours(hrs, min, sec, 0);
273: }-*/;
274:
275: private native void init(long date) /*-{
276: this.jsdate = new Date(date);
277: }-*/;
278: }
|