KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > exslt > Date


1 package net.sf.saxon.exslt;
2 import java.text.SimpleDateFormat JavaDoc;
3 import java.util.Calendar JavaDoc;
4 import java.util.GregorianCalendar JavaDoc;
5
6 /**
7 * This class implements extension functions in the
8 * http://exslt.org/dates-and-times namespace. <p>
9 */

10
11 public final class Date {
12
13     /**
14     * Private constructor to disallow instantiation
15     */

16
17     private Date() {};
18
19     /**
20     * The date:date-time function returns the current date and time as a date/time string.
21     * The date/time string that's returned must be a string in the format defined as the
22     * lexical representation of xs:dateTime in [3.2.7 dateTime] of [XML Schema Part 2: Datatypes].
23     * The date/time format is basically CCYY-MM-DDThh:mm:ss+hh:mm.
24     * The date/time string format must include a time zone, either a Z to indicate
25     * Coordinated Universal Time or a + or - followed by the difference between the
26     * difference from UTC represented as hh:mm.
27     */

28
29     public static String JavaDoc dateTime() {
30         Calendar JavaDoc calendar = new GregorianCalendar JavaDoc();
31         int tzoffset = calendar.get(Calendar.ZONE_OFFSET) +
32                         calendar.get(Calendar.DST_OFFSET);
33         char sign = '+';
34         if (tzoffset < 0) {
35             sign = '-';
36             tzoffset = -tzoffset;
37         }
38         int tzminutes = tzoffset / 60000;
39         int tzhours = tzminutes / 60;
40         tzminutes = tzminutes % 60;
41         String JavaDoc tzh = "" + tzhours;
42         while (tzh.length() < 2) tzh = "0" + tzh;
43         String JavaDoc tzm = "" + tzminutes;
44         while (tzm.length() < 2) tzm = "0" + tzm;
45
46         SimpleDateFormat JavaDoc formatter
47             = new SimpleDateFormat JavaDoc ("yyyy-MM-dd'T'HH:mm:ss");
48         String JavaDoc base = formatter.format(new java.util.Date JavaDoc());
49         return base + sign + tzh + ':' + tzm;
50     }
51
52     /**
53     * The date:date function returns the date specified in the date/time string given as the
54     * argument.
55     * @param dateTime must start with [+|-]CCYY-MM-DD
56     */

57
58     public static String JavaDoc date(String JavaDoc dateTime) {
59         int offset = 0;
60         if (dateTime.length() >= 1 &&
61                 (dateTime.charAt(0) == '-' || dateTime.charAt(0) == '+')) {
62             offset = 1;
63         }
64         if (dateTime.length() >= offset+10) {
65             return dateTime.substring(0, offset+10);
66         } else {
67             return "";
68         }
69     }
70
71     /**
72     * The date:date function returns the current date.
73     */

74
75     public static String JavaDoc date() {
76         return date(dateTime());
77     }
78
79     /**
80     * The date:time function returns the time specified in the date/time string given as the
81     * argument.
82     * @param dateTime must start with [+|-]CCYY-MM-DDThh:mm:ss
83     */

84
85     public static String JavaDoc time(String JavaDoc dateTime) {
86         int t = dateTime.indexOf('T');
87         if (t<0 || t==dateTime.length()-1) {
88             return "";
89         } else {
90             return dateTime.substring(t+1);
91         }
92     }
93
94     /**
95     * The date:time function returns the current time.
96     */

97
98     public static String JavaDoc time() {
99         return time(dateTime());
100     }
101
102     /**
103     * The date:year function returns the year specified in the date/time string given as the
104     * argument.
105     * @param dateTime must begin with CCYY
106     */

107
108     public static double year(String JavaDoc dateTime) {
109         if (dateTime.startsWith("-")) {
110             return Double.NaN;
111         }
112         try {
113             return (double)Integer.parseInt(dateTime.substring(0, 4));
114         } catch (Exception JavaDoc err) {
115             return Double.NaN;
116         }
117     }
118
119     /**
120     * The date:year function returns the current year.
121     */

122
123     public static double year() {
124         return year(dateTime());
125     }
126
127     /**
128     * Return true if the year specified in the date/time string
129     * given as the argument is a leap year.
130     */

131
132     public static boolean leapYear(String JavaDoc dateTime) {
133         double year = year(dateTime);
134         if (Double.isNaN(year)) {
135             return false;
136         }
137         int y = (int)year;
138         return (y % 4 == 0) && !((y % 100 == 0) && !(y % 400 == 0));
139     }
140
141     /**
142     * Returns true if the current year is a leap year
143     */

144
145     public static boolean leapYear() {
146         return leapYear(dateTime());
147     }
148
149     /**
150     * Return the month number from a date.
151     * The date must start with either "CCYY-MM" or "--MM"
152     */

153
154     public static double monthInYear(String JavaDoc dateTime) {
155         try {
156             if (dateTime.startsWith("--")) {
157                 return (double)Integer.parseInt(dateTime.substring(2, 4));
158             } else {
159                 if (dateTime.indexOf('-')!=4) {
160                     return Double.NaN;
161                 }
162                 return (double)Integer.parseInt(dateTime.substring(5, 7));
163             }
164         } catch (Exception JavaDoc err) {
165             return Double.NaN;
166         }
167     }
168
169     /**
170     * Return the month number from the current date.
171     */

172
173     public static double monthInYear() {
174         return monthInYear(date());
175     }
176
177     /**
178     * Return the month name from a date.
179     * The date must start with either "CCYY-MM" or "--MM"
180     * @return the English month name, for example "January", "February"
181     */

182
183     public static String JavaDoc monthName(String JavaDoc date) {
184         String JavaDoc[] months = {"January", "February", "March", "April", "May", "June",
185                             "July", "August", "September", "October", "November", "December"};
186         double m = monthInYear(date);
187         if (Double.isNaN(m)) {
188             return "";
189         }
190         return months[(int)m - 1];
191     }
192
193     /**
194     * Return the month name from the current date.
195     * @return the English month name, for example "January", "February"
196     */

197
198     public static String JavaDoc monthName() {
199         return monthName(date());
200     }
201
202     /**
203     * Return the month abbreviation from a date.
204     * The date must start with either "CCYY-MM" or "--MM"
205     * @return the English month abbreviation, for example "Jan", "Feb"
206     */

207
208     public static String JavaDoc monthAbbreviation(String JavaDoc date) {
209         String JavaDoc[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
210                             "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
211         double m = monthInYear(date);
212         if (Double.isNaN(m)) {
213             return "";
214         }
215         return months[(int)m - 1];
216     }
217
218     /**
219     * Return the month abbreviation from the current date.
220     * @return the English month abbreviation, for example "Jan", "Feb"
221     */

222
223     public static String JavaDoc monthAbbreviation() {
224         return monthAbbreviation(date());
225     }
226
227     /**
228     * Return the ISO week number of a specified date within the year
229     * (Note, this returns the ISO week number: the result in EXSLT is underspecified)
230     */

231
232     public static double weekInYear(String JavaDoc dateTime) {
233         int dayInYear = (int)dayInYear(dateTime);
234         String JavaDoc firstJan = dateTime.substring(0,4) + "-01-01";
235         int jan1day = ((int)dayInWeek(firstJan) + 5) % 7;
236         int daysInFirstWeek = (jan1day==0 ? 0 : 7 - jan1day);
237
238         int rawWeek = (dayInYear - daysInFirstWeek + 6) / 7;
239
240         if (daysInFirstWeek >= 4) {
241             return rawWeek + 1;
242         } else {
243             if (rawWeek>0) {
244                 return rawWeek;
245             } else {
246                 // week number should be 52 or 53: same as 31 Dec in previous year
247
int lastYear = Integer.parseInt(dateTime.substring(0,4)) - 1;
248                 String JavaDoc dec31 = lastYear + "-12-31";
249                     // assumes year > 999
250
return weekInYear(dec31);
251             }
252         }
253     }
254
255     /**
256     * Return the ISO week number of the current date within the year
257     * (Note, this returns the ISO week number: the result in EXSLT is underspecified)
258     */

259
260     public static double weekInYear() {
261         return weekInYear(date());
262     }
263
264     /**
265     * Return the week number of a specified date within the month
266     * (Note, this function is underspecified in EXSLT)
267     */

268
269     public static double weekInMonth(String JavaDoc dateTime) {
270         return (double)(int)((dayInMonth(dateTime)-1) / 7 + 1);
271     }
272
273     /**
274     * Return the ISO week number of the current date within the month
275     */

276
277     public static double weekInMonth() {
278         return weekInMonth(date());
279     }
280
281     /**
282     * Return the day number of a specified date within the year
283     */

284
285     public static double dayInYear(String JavaDoc dateTime) {
286         int month=(int)monthInYear(dateTime);
287         int day = (int)dayInMonth(dateTime);
288         int[] prev = { 0,
289                         31,
290                         31+28,
291                         31+28+31,
292                         31+28+31+30,
293                         31+28+31+30+31,
294                         31+28+31+30+31+30,
295                         31+28+31+30+31+30+31,
296                         31+28+31+30+31+30+31+31,
297                         31+28+31+30+31+30+31+31+30,
298                         31+28+31+30+31+30+31+31+30+31,
299                         31+28+31+30+31+30+31+31+30+31+30,
300                         31+28+31+30+31+30+31+31+30+31+30+31 };
301         int leap = (month>2 && leapYear(dateTime) ? 1 : 0);
302         return prev[month-1] + leap + day;
303     }
304
305     /**
306     * Return the day number of the current date within the year
307     */

308
309     public static double dayInYear() {
310         return dayInYear(date());
311     }
312
313     /**
314     * Return the day number of a specified date within the month
315     * @param dateTime must start with CCYY-MM-DD, or --MM-DD, or ---DD
316     */

317
318     public static double dayInMonth(String JavaDoc dateTime) {
319         try {
320             if (dateTime.startsWith("---")) {
321                 return (double)Integer.parseInt(dateTime.substring(3,5));
322             } else if (dateTime.startsWith("--")) {
323                 return (double)Integer.parseInt(dateTime.substring(5,7));
324             } else {
325                 return (double)Integer.parseInt(dateTime.substring(8,10));
326             }
327         } catch (Exception JavaDoc err) {
328             return Double.NaN;
329         }
330     }
331
332     /**
333     * Return the day number of the current date within the month
334     */

335
336     public static double dayInMonth() {
337         return dayInMonth(date());
338     }
339
340     /**
341     * Return the day-of-the-week in a month of a date as a number
342     * (for example 3 for the 3rd Tuesday in May).
343     * @param dateTime must start with CCYY-MM-DD
344     */

345
346     public static double dayOfWeekInMonth(String JavaDoc dateTime) {
347         double dd = dayInMonth(dateTime);
348         if (Double.isNaN(dd)) {
349             return dd;
350         }
351         return (((int)dd) - 1) / 7 + 1;
352     }
353
354     /**
355     * Return the day-of-the-week in a month of the current date as a number
356     * (for example 3 for the 3rd Tuesday in May).
357     */

358
359     public static double dayOfWeekInMonth() {
360         return dayOfWeekInMonth(date());
361     }
362
363     /**
364     * Return the day of the week given in a date as a number.
365     * The numbering of days of the week starts at 1 for Sunday, 2 for Monday
366     * and so on up to 7 for Saturday.
367     * @param dateTime must start with CCYY-MM-DD
368     */

369
370     public static double dayInWeek(String JavaDoc dateTime) {
371         double yy = year(dateTime);
372         double mm = monthInYear(dateTime);
373         double dd = dayInMonth(dateTime);
374         if (Double.isNaN(yy) || Double.isNaN(mm) || Double.isNaN(dd)) {
375             return Double.NaN;
376         }
377         GregorianCalendar JavaDoc calDate =
378             new GregorianCalendar JavaDoc(
379                         (int)yy,
380                         (int)mm-1,
381                         (int)dd);
382         calDate.setFirstDayOfWeek(Calendar.SUNDAY);
383         return calDate.get(Calendar.DAY_OF_WEEK);
384     }
385
386     /**
387     * Return the day of the week in the current date as a number.
388     * The numbering of days of the week starts at 1 for Sunday, 2 for Monday
389     * and so on up to 7 for Saturday.
390     */

391
392     public static double dayInWeek() {
393         return dayInWeek(date());
394     }
395
396     /**
397     * Return the day of the week given in a date as an English day name:
398     * one of 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday' or 'Friday'.
399     * @param dateTime must start with CCYY-MM-DD
400     */

401
402     public static String JavaDoc dayName(String JavaDoc dateTime) {
403         String JavaDoc[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
404                             "Saturday" };
405         double d = dayInWeek(dateTime);
406         if (Double.isNaN(d)) {
407             return "";
408         }
409         return days[(int)d - 1];
410     }
411
412     /**
413     * Return the day of the week given in the current date as an English day name:
414     * one of 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday' or 'Friday'.
415     */

416
417     public static String JavaDoc dayName() {
418         return dayName(date());
419     }
420
421     /**
422     * Return the day of the week given in a date as an English day abbreviation:
423     * one of 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', or 'Sat'.
424     * @param dateTime must start with CCYY-MM-DD
425     */

426
427     public static String JavaDoc dayAbbreviation(String JavaDoc dateTime) {
428         String JavaDoc[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
429         double d = dayInWeek(dateTime);
430         if (Double.isNaN(d)) {
431             return "";
432         }
433         return days[(int)d - 1];
434     }
435
436     /**
437     * Return the day of the week given in the current date as an English day abbreviation:
438     * one of 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', or 'Sat'.
439     */

440
441     public static String JavaDoc dayAbbreviation() {
442         return dayAbbreviation(date());
443     }
444
445     /**
446     * Return the hour of the day in the specified date or date/time
447     * @param dateTime must start with CCYY-MM-DDThh:mm:ss or hh:mm:ss
448     */

449
450     public static double hourInDay(String JavaDoc dateTime) {
451         int t = dateTime.indexOf('T');
452         try {
453             int hh = Integer.parseInt(dateTime.substring(t+1, t+3));
454             return (double)hh;
455         } catch (Exception JavaDoc err) {
456             return Double.NaN;
457         }
458     }
459
460     /**
461     * Return the current hour of the day
462     */

463
464     public static double hourInDay() {
465         return hourInDay(time());
466     }
467
468     /**
469     * Return the minute of the hour in the specified date or date/time
470     * @param dateTime must start with CCYY-MM-DDThh:mm:ss or hh:mm:ss
471     */

472
473     public static double minuteInHour(String JavaDoc dateTime) {
474         int t = dateTime.indexOf('T');
475         try {
476             int mm = Integer.parseInt(dateTime.substring(t+4, t+6));
477             return (double)mm;
478         } catch (Exception JavaDoc err) {
479             return Double.NaN;
480         }
481     }
482
483     /**
484     * Return the current minute of the hour
485     */

486
487     public static double minuteInHour() {
488         return minuteInHour(time());
489     }
490
491     /**
492     * Return the second of the minute in the specified date or date/time
493     * @param dateTime must start with CCYY-MM-DDThh:mm:ss or hh:mm:ss
494     */

495
496     public static double secondInMinute(String JavaDoc dateTime) {
497         int t = dateTime.indexOf('T');
498         try {
499             int ss = Integer.parseInt(dateTime.substring(t+7, t+9));
500             return (double)ss;
501         } catch (Exception JavaDoc err) {
502             return Double.NaN;
503         }
504     }
505
506     /**
507     * Return the current second of the minute
508     */

509
510     public static double secondInMinute() {
511         return secondInMinute(time());
512     }
513
514 }
515
516
517
518
519
520 //
521
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
522
// you may not use this file except in compliance with the License. You may obtain a copy of the
523
// License at http://www.mozilla.org/MPL/
524
//
525
// Software distributed under the License is distributed on an "AS IS" basis,
526
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
527
// See the License for the specific language governing rights and limitations under the License.
528
//
529
// The Original Code is: all this file.
530
//
531
// The Initial Developer of the Original Code is Michael H. Kay.
532
//
533
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
534
//
535
// Contributor(s): none.
536
//
537
Popular Tags