001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.util;
010:
011: import java.util.Date;
012:
013: /**
014: * The date parsing and format syntax is a subset of PHP's date() function, and the formats that are supported will provide results equivalent to their PHP versions. Following is the list of all currently supported formats:
015: * <p/>
016: * <p/>
017: * <pre>
018: * <p/>
019: * Sample date:
020: * 'Wed Jan 10 2007 15:05:01 GMT-0600 (Central Standard Time)'
021: * <p/>
022: * Format Description Example returned values
023: * ------ ----------------------------------------------------------------------- -----------------------
024: * d Day of the month, 2 digits with leading zeros 01 to 31
025: * D A short textual representation of the day of the week Mon to Sun
026: * j Day of the month without leading zeros 1 to 31
027: * l A full textual representation of the day of the week Sunday to Saturday
028: * N ISO-8601 numeric representation of the day of the week 1 (for Monday) through 7 (for Sunday)
029: * S English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j
030: * w Numeric representation of the day of the week 0 (for Sunday) to 6 (for Saturday)
031: * z The day of the year (starting from 0) 0 to 364 (365 in leap years)
032: * W ISO-8601 week number of year, weeks starting on Monday 01 to 53
033: * F A full textual representation of a month, such as January or March January to December
034: * m Numeric representation of a month, with leading zeros 01 to 12
035: * M A short textual representation of a month Jan to Dec
036: * n Numeric representation of a month, without leading zeros 1 to 12
037: * t Number of days in the given month 28 to 31
038: * L Whether it's a leap year 1 if it is a leap year, 0 otherwise.
039: * o ISO-8601 year number (identical to (Y), but if the ISO week number (W) Examples: 1998 or 2004
040: * belongs to the previous or next year, that year is used instead)
041: * Y A full numeric representation of a year, 4 digits Examples: 1999 or 2003
042: * y A two digit representation of a year Examples: 99 or 03
043: * a Lowercase Ante meridiem and Post meridiem am or pm
044: * A Uppercase Ante meridiem and Post meridiem AM or PM
045: * g 12-hour format of an hour without leading zeros 1 to 12
046: * G 24-hour format of an hour without leading zeros 0 to 23
047: * h 12-hour format of an hour with leading zeros 01 to 12
048: * H 24-hour format of an hour with leading zeros 00 to 23
049: * i Minutes, with leading zeros 00 to 59
050: * s Seconds, with leading zeros 00 to 59
051: * u Milliseconds, with leading zeros 001 to 999
052: * O Difference to Greenwich time (GMT) in hours and minutes Example: +1030
053: * P Difference to Greenwich time (GMT) with colon between hours and minutes Example: -08:00
054: * T Timezone abbreviation of the machine running the code Examples: EST, MDT, PDT ...
055: * Z Timezone offset in seconds (negative if west of UTC, positive if east) -43200 to 50400
056: * c ISO 8601 date 2007-04-17T15:19:21+08:00
057: * U Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) 1193432466 or -2138434463
058: * <p/>
059: * </pre>
060: */
061: public class DateUtil {
062:
063: public static class Interval {
064: private String interval;
065:
066: private Interval(String direction) {
067: this .interval = direction;
068: }
069:
070: public String getInterval() {
071: return interval;
072: }
073: }
074:
075: public static Interval MILLI = new Interval("ms");
076: public static Interval SECOND = new Interval("s");
077: public static Interval MINUTE = new Interval("mi");
078: public static Interval HOUR = new Interval("h");
079: public static Interval DAY = new Interval("d");
080: public static Interval MONTH = new Interval("mo");
081: public static Interval YEAR = new Interval("y");
082:
083: /**
084: * Allocates a <code>Date</code> object and initializes it to represent the specified number of milliseconds since the
085: * standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
086: *
087: * @param time the time in milliseconds
088: * @return date
089: */
090: public static Date create(long time) {
091: return new Date(time);
092: }
093:
094: /**
095: * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
096: *
097: * @param date the date
098: * @return time in milliseconds
099: */
100: public static long getTime(Date date) {
101: return date.getTime();
102: }
103:
104: /**
105: * Formats a date given the supplied format string. You can also use GWT's standard {@link com.google.gwt.i18n.client.DateTimeFormat} class.
106: *
107: * @param date the date
108: * @param format the format string
109: * @return the formatted date
110: * @see com.google.gwt.i18n.client.DateTimeFormat
111: */
112: public static native String format(Date date, String format) /*-{
113: if(date == null) return "";
114: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
115: return new $wnd.Date(millis).format(format).toString();
116: }-*/;
117:
118: /**
119: * Parses the passed string using the specified format. Note that this function expects dates in normal calendar format,
120: * meaning that months are 1-based (1 = January) and not zero-based like in JavaScript dates. Any part of the date format
121: * that is not specified will default to the current date value for that part. Time parts can also be specified, but default to 0.
122: * Keep in mind that the input date string must precisely match the specified format string or the parse operation will fail.
123: *
124: * @param input the unparsed date as a string
125: * @param format the format string
126: * @return the parsed date
127: */
128: public static native Date parseDate(String input, String format)/*-{
129: var date = $wnd.Date.parseDate(input, format);
130: return @com.gwtext.client.util.DateUtil::create(J)(date.getTime());
131: }-*/;
132:
133: /**
134: * Provides a convenient method of performing basic date arithmetic. This method does not modify the Date instance
135: * being called - it creates and returns a new Date instance containing the resulting date value.
136: *
137: * @param date the date
138: * @param interval the interval enum
139: * @param value the value to add
140: * @return the new Date
141: */
142: public static native Date add(Date date, Interval interval,
143: int value)/*-{
144: var intervalJS = interval.@com.gwtext.client.util.DateUtil.Interval::getInterval()();
145: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
146: var dateJS = new $wnd.Date(millis);
147: var newDate = dateJS.add(intervalJS, value);
148: return @com.gwtext.client.util.DateUtil::create(J)(newDate.getTime());
149: }-*/;
150:
151: /**
152: * Clears any time information from this date.
153: *
154: * @param date the date
155: * @return the new date
156: */
157: public static native Date clearTime(Date date)/*-{
158: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
159: var dateJS = new $wnd.Date(millis);
160: var newDate = date.clearTime();
161: return @com.gwtext.client.util.DateUtil::create(J)(newDate.getTime());
162: }-*/;
163:
164: /**
165: * Get the numeric day number of the year, adjusted for leap year.
166: *
167: * @param date the date
168: * @return 0 through 364 (365 in leap years)
169: */
170: public static native int getDayOfYear(Date date) /*-{
171: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
172: var dateJS = new $wnd.Date(millis);
173: return dateJS.getDayOfYear();
174: }-*/;
175:
176: /**
177: * Get the number of days in the current month, adjusted for leap year.
178: *
179: * @param date the date
180: * @return the number of days in the month
181: */
182: public static native int getDaysInMonth(Date date) /*-{
183: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
184: var dateJS = new $wnd.Date(millis);
185: return dateJS.getDaysInMonth();
186: }-*/;
187:
188: /**
189: * Get the first date of this date's month
190: *
191: * @param date the date
192: * @return the new date
193: */
194: public static native Date getFirstDateOfMonth(Date date) /*-{
195: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
196: var dateJS = new $wnd.Date(millis);
197: var newDate = dateJS.getFirstDateOfMonth();
198: return @com.gwtext.client.util.DateUtil::create(J)(newDate.getTime());
199: }-*/;
200:
201: /**
202: * Get the first day of the current month, adjusted for leap year. The returned value is the numeric day index within the week (0-6).
203: *
204: * @param date the date
205: * @return the day number (0-6)
206: */
207: public static native int getFirstDayOfMonth(Date date) /*-{
208: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
209: var dateJS = new $wnd.Date(millis);
210: return dateJS.getFirstDayOfMonth();
211: }-*/;
212:
213: /**
214: * Get the offset from GMT of the current date.
215: *
216: * @param date the date
217: * @return the 4-character offset string prefixed with + or - (e.g. '-0600')
218: */
219: public static native String getGMTOffset(Date date) /*-{
220: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
221: var dateJS = new $wnd.Date(millis);
222: return dateJS.getGMTOffset();
223: }-*/;
224:
225: /**
226: * Get the last date of this date's month.
227: *
228: * @param date the date
229: * @return the new date
230: */
231: public static native Date getLastDateOfMonth(Date date) /*-{
232: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
233: var dateJS = new $wnd.Date(millis);
234: var newDate = dateJS.getLastDateOfMonth();
235: return @com.gwtext.client.util.DateUtil::create(J)(newDate.getTime());
236: }-*/;
237:
238: /**
239: * Get the last day of the current month, adjusted for leap year. The returned value is the numeric day index within the week (0-6).
240: *
241: * @param date the date
242: * @return the day number (0-6)
243: */
244: public static native int getLastDayOfMonth(Date date) /*-{
245: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
246: var dateJS = new $wnd.Date(millis);
247: return dateJS.getLastDayOfMonth();
248: }-*/;
249:
250: /**
251: * Get the timezone abbreviation of the current date.
252: *
253: * @param date the date
254: * @return the abbreviated timezone name (e.g. 'CST')
255: */
256: public static native String getTimezone(Date date) /*-{
257: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
258: var dateJS = new $wnd.Date(millis);
259: return dateJS.getTimezone();
260: }-*/;
261:
262: //todo ext function incorrect : see http://extjs.com/forum/showthread.php?t=9727
263: /**
264: * Get the string representation of the numeric week number of the year.
265: *
266: * @param date the date
267: * @return 0 through 52
268: */
269: public static native int getWeekOfYear(Date date) /*-{
270: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
271: var dateJS = new $wnd.Date(millis);
272: //returns string
273: var woy = dateJS.getWeekOfYear();
274: return @java.lang.Integer::parseInt(Ljava/lang/String;)(woy);
275: }-*/;
276:
277: /**
278: * Whether or not the current date is in a leap year.
279: *
280: * @param date the date
281: * @return true if date is in a leap year
282: */
283: public static native boolean isLeapYear(Date date) /*-{
284: var millis = @com.gwtext.client.util.DateUtil::getTime(Ljava/util/Date;)(date);
285: var dateJS = new $wnd.Date(millis);
286: return dateJS.isLeapYear();
287: }-*/;
288:
289: }
|