KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > exslt > Date


1 package com.icl.saxon.exslt;
2 import java.util.Calendar JavaDoc;
3 import java.util.GregorianCalendar JavaDoc;
4 import java.text.SimpleDateFormat 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:ssTZ.
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, e.g. "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, e.g. "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, e.g. "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, e.g. "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         int weekDay = ((int)dayInWeek(dateTime) + 5) % 7;
235                     // normalized so Mon=0, Sun=6
236
String JavaDoc firstJan = dateTime.substring(0,4) + "-01-01";
237         int jan1day = ((int)dayInWeek(firstJan) + 5) % 7;
238         int daysInFirstWeek = (jan1day==0 ? 0 : 7 - jan1day);
239                     
240         int rawWeek = (dayInYear - daysInFirstWeek + 6) / 7;
241
242         if (daysInFirstWeek >= 4) {
243             return rawWeek + 1;
244         } else {
245             if (rawWeek>0) {
246                 return rawWeek;
247             } else {
248                 // week number should be 52 or 53: same as 31 Dec in previous year
249
int lastYear = Integer.parseInt(dateTime.substring(0,4)) - 1;
250                 String JavaDoc dec31 = lastYear + "-12-31";
251                     // assumes year > 999
252
return weekInYear(dec31);
253             }
254         }
255     }
256
257     /**
258     * Return the ISO week number of the current date within the year
259     * (Note, this returns the ISO week number: the result in EXSLT is underspecified)
260     */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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