KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > DateSupport


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9  */

10 package org.mmbase.util;
11
12 import java.text.*;
13 import java.text.DateFormat JavaDoc;
14 import java.text.SimpleDateFormat JavaDoc;
15 import java.util.*;
16 import java.util.Calendar JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.GregorianCalendar JavaDoc;
19 import java.util.SimpleTimeZone JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21 import java.util.TimeZone JavaDoc;
22
23 import org.mmbase.util.logging.Logger;
24 import org.mmbase.util.logging.Logging;
25
26 /**
27  * Some routines to support dates better<br /><br />
28  *
29  * The problem that generally occurs is with timezones. Therefore, we have made the following structure:<br />
30  * <ul>
31  * <li> If a date is stored in a database, it is in GMT
32  * <li> If a date is displayed, it happens in the timezone of the machine that is calling.
33  * </ul>
34  * This means that some timezone conversions have to be made.
35  * We assume nothing about timezones, we just read the value specified by the system (Timezone.getDefault() call).
36  *
37  * @deprecated use Calendar and java.util.DateFormat
38  * @author Rico Jansen
39  * @author Johannes Verelst
40  * @version $Id: DateSupport.java,v 1.26 2005/10/05 10:44:00 michiel Exp $
41  */

42 public class DateSupport {
43
44     // logger
45
private static Logger log = Logging.getLoggerInstance(DateSupport.class);
46
47     /**
48      * The offset for date conversions for the current zone, in seconds
49      */

50     static int offset = 0;
51     /**
52      * Whether to sue the offset.
53      * Set to <code>true</code> when initialization of this class succeeds.
54      */

55     static boolean dooffset = true;
56
57     /**
58     * Return the number of days in the month in a specified year.
59     * Leap years have to be taken into account
60     *
61     * @param year The year valid values 0..100 where 0 is y2k 2000 untill 89 => 2089 and 90 being the year 1990
62     * @param month The month where 0 is januari
63     */

64     static public int daysInMonth(int year, int month) {
65         int months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
66         int days = months[month];
67         year = (year < 90) ? year + 2000 : year + 1900;
68
69         // Make an exception for the intercalary day.
70
if (month == 1) {
71             if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
72                 days = 29;
73         }
74         return days;
75     }
76
77     /**
78      * Return the number of seconds that have elapsed from the beginning of the year to the given date
79      *
80      * @param d The date
81      * @return The number of secods from January 1 to the given date
82      * @see DateSupport#daysInMonth
83      * @see DateSupport#dayInYear
84      * @see DateSupport#weekInYear
85      */

86     static public int secondInYear(Date JavaDoc d) {
87         Calendar JavaDoc c = Calendar.getInstance();
88         c.setTime(d);
89         return c.get(Calendar.DAY_OF_YEAR) * (60 * 60 * 24) + c.get(Calendar.HOUR_OF_DAY) * (24 * 60) + c.get(Calendar.SECOND);
90     }
91
92     /**
93      * Return the number of days that have elapsed from the beginning of the year to the given date
94      *
95      * @param d The date
96      * @return The number of days from January 1 to the given date
97      * @see DateSupport#daysInMonth
98      * @see DateSupport#secondInYear
99      * @see DateSupport#weekInYear
100      */

101     static public int dayInYear(Date JavaDoc d) {
102         Calendar JavaDoc c = Calendar.getInstance();
103         c.setTime(d);
104         return c.get(Calendar.DAY_OF_YEAR);
105     }
106
107     /**
108      * Return the number of weeks that have elapsed from the beginning of the year to the given date
109      *
110      * @param d The date
111      * @return The number of weeks from January 1 to the given date
112      * @see DateSupport#daysInMonth
113      * @see DateSupport#secondInYear
114      * @see DateSupport#dayInYear
115      */

116     static public int weekInYear(Date JavaDoc d) {
117         Calendar JavaDoc c = Calendar.getInstance();
118         c.setTime(d);
119         return c.get(Calendar.WEEK_OF_YEAR);
120     }
121
122     /**
123      * Return the number milliseconds elapsed from 1-Jan-1970 to the beginning of the given week.
124      *
125      * @param year The year
126      * @param week The number of the week
127      * @return The number of milliseconds between 1-Jan-1970 and the begin of the given week.
128      */

129     static public long milliDate(int year, int week) {
130         Calendar JavaDoc calendar = Calendar.getInstance();
131         calendar.set(year, 0, 0);
132         calendar.set(Calendar.WEEK_OF_YEAR, week);
133         Date JavaDoc d = calendar.getTime();
134         return d.getTime();
135     }
136
137     /**
138      * Return a date, based on a year, a week and the day of that week <br />
139      * For instance: 1999, 40, 4 = The 4th day of the 40th week of 1999
140      *
141      * @param year The year
142      * @param week The week
143      * @param day The number of the day in the week
144      * @return A date-object for the given date
145      */

146     static public Date JavaDoc Date(int year, int week, int day) {
147         Calendar JavaDoc cal = Calendar.getInstance();
148         cal.set(Calendar.YEAR, year);
149         cal.set(Calendar.WEEK_OF_YEAR, week);
150         cal.set(Calendar.DAY_OF_WEEK, day);
151         return cal.getTime();
152     }
153
154     /**
155      * Create date strings in the form yyyy-mm-dd for a given Date object
156      * <br />This format is used in several database (dbm's)
157      * @param da The date input
158      * @return A string in the form yyyy-mm-dd
159      * @see DateSupport#parsedbmdate
160      */

161     public static String JavaDoc makedbmdate(Date JavaDoc da) {
162         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc("yyyy-MM-dd", Locale.ENGLISH);
163         return f.format(da);
164     }
165
166     /**
167      * Parse date strings in the form yyyy-mm-dd
168      * <br />This format is used in several database (dbm's)
169      * @param wh The string representing the date in 'yyyy-mm-dd' format
170      * @return A Date object for the given date
171      * @see DateSupport#makedbmdate
172      */

173     public static Date JavaDoc parsedbmdate(String JavaDoc wh) {
174         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc("yyyy-MM-dd", Locale.ENGLISH);
175
176         //for bw comp
177
if (wh.indexOf('/') != -1) {
178             f = new SimpleDateFormat JavaDoc("yyyy/MM/dd", Locale.ENGLISH);
179         }
180         try {
181             return f.parse(wh);
182         } catch (ParseException e) {
183             //idem for bw comp
184
return new Date JavaDoc();
185         }
186     }
187
188     /**
189      * Puts a colon between a time of RFC-1223 format
190      *
191      * @param time A string in RFC-1223 format
192      * @return A string with an extra colon
193      */

194     public static String JavaDoc colontime(String JavaDoc time) {
195         if (time.length() == 4) {
196             return time.substring(0, 2) + ":" + time.substring(2, 4);
197         }
198         return time;
199     }
200
201     /**
202      * Returns the number of seconds from 1-Jan-1970 to a given date
203      *
204      * @param sDate String in the form 'yyyyMMdd'
205      * @return Number of seconds from 1-Jan-1970
206      * @see DateSupport#parsetime
207      * @see DateSupport#parsedatetime
208      */

209     public static int parsedate(String JavaDoc sDate) {
210         SimpleDateFormat JavaDoc df = (SimpleDateFormat JavaDoc) DateFormat.getDateTimeInstance();
211         TimeZone JavaDoc tz;
212         df.applyLocalizedPattern("yyyyMMdd");
213
214         tz = TimeZone.getDefault();
215         df.setTimeZone(tz);
216
217         Date JavaDoc date = null;
218         try {
219             date = df.parse(sDate);
220         } catch (java.text.ParseException JavaDoc e) {
221             log.error(e.toString());
222         }
223
224         if (date != null)
225             return (int) ((date.getTime() - getMilliOffset()) / 1000);
226         else
227             return -1;
228     }
229
230     /**
231      * Returns the number of seconds from 00:00:00 to a given time
232      *
233      * @param wh Time in the form 'hhmmss'
234      * @return Number of seconds from 00:00:00 to the given time
235      * @see DateSupport#parsedate
236      * @see DateSupport#parsedatetime
237      */

238     public static int parsetime(String JavaDoc wh) {
239         int h = 0, m = 0, s = 0;
240         try {
241             h = Integer.parseInt(wh.substring(0, 2));
242             m = Integer.parseInt(wh.substring(2, 4));
243             s = Integer.parseInt(wh.substring(4, 6));
244         } catch (Exception JavaDoc e) {
245             log.error("DateSupport: maketime (" + wh + ")");
246         }
247         return s + 60 * (m + 60 * h);
248     }
249
250     /**
251      * Returns the number of seconds from 1-Jan-1970 00:00:00 to a given time
252      *
253      * @param wh Date in the form 'yyyymmddhhmmss'
254      * @return Number of seconds from 1-Jan-1970 00:00:00 to the given time
255      * @see DateSupport#parsedate
256      * @see DateSupport#parsetime
257      */

258     public static int parsedatetime(String JavaDoc wh) {
259         return parsedate(wh.substring(0, 8)) + parsetime(wh.substring(8, 14));
260     }
261
262     /**
263      * Takes an integer representing the number of seconds from 1-Jan-1970 00:00:00 and returns the time as a string
264      *
265      * @param val Number of seconds from 1-Jan-1970 00:00:00
266      * @return String in the form 'hhmm' for the given time
267      * @see DateSupport#getTimeSec
268      * @see DateSupport#getTimeSecLen
269      * @see DateSupport#getMonthDay
270      * @see DateSupport#getMonth
271      * @see DateSupport#getYear
272      * @see DateSupport#getMonthInt
273      * @see DateSupport#getWeekDayInt
274      * @see DateSupport#getDayInt
275      */

276     public static String JavaDoc getTime(int val) {
277         if (dooffset) {
278             val += offset;
279         }
280         Date JavaDoc v = new Date JavaDoc((long) val * 1000);
281         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc("HH:mm");
282         return f.format(v);
283     }
284
285     /**
286      * Takes an integer representing the number of seconds from 1-Jan-1970 00:00:00 and returns the time as a string
287      *
288      * @param val Number of seconds from 1-Jan-1970 00:00:00
289      * @return String in the form 'hhmmss' for the given time
290      * @see DateSupport#getTime
291      * @see DateSupport#getTimeSecLen
292      * @see DateSupport#getMonthDay
293      * @see DateSupport#getMonth
294      * @see DateSupport#getYear
295      * @see DateSupport#getMonthInt
296      * @see DateSupport#getWeekDayInt
297      * @see DateSupport#getDayInt
298      */

299     public static String JavaDoc getTimeSec(int val) {
300         Date JavaDoc v;
301         if (val == -1) {
302             // WHY? This behaviour leads to incorrect displaying of MMEvents!!
303
v = new Date JavaDoc();
304         } else {
305             if (dooffset) {
306                 val += offset;
307             }
308             v = new Date JavaDoc((long) val * 1000);
309         }
310         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc("HH:mm:ss");
311         return f.format(v);
312     }
313
314     /**
315      * Takes an integer representing the number of seconds from 00:00:00 and returns the time as a string
316      *
317      * @param val Number of seconds from 00:00:00
318      * @return String in the form 'hhmmss' for the given time
319      * @see DateSupport#getTime
320      * @see DateSupport#getTimeSec
321      * @see DateSupport#getMonthDay
322      * @see DateSupport#getMonth
323      * @see DateSupport#getYear
324      * @see DateSupport#getMonthInt
325      * @see DateSupport#getWeekDayInt
326      * @see DateSupport#getDayInt
327      */

328     public static String JavaDoc getTimeSecLen(int val) {
329         Date JavaDoc date = new Date JavaDoc((long) val * 1000);
330         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc("HH:mm:ss");
331         f.setTimeZone(TimeZone.getTimeZone("GMT"));
332         return f.format(date);
333     }
334
335     /**
336      * Takes an integer representing the number of seconds from 1-Jan-1970 00:00:00 and returns the day in the month
337      *
338      * @param val Number of seconds from 1-Jan-1970 00:00:00
339      * @return String containing the day of the month (1 to 31)
340      * @see DateSupport#getTime
341      * @see DateSupport#getTimeSec
342      * @see DateSupport#getTimeSecLen
343      * @see DateSupport#getMonth
344      * @see DateSupport#getYear
345      * @see DateSupport#getMonthInt
346      * @see DateSupport#getWeekDayInt
347      * @see DateSupport#getDayInt
348      */

349     public static String JavaDoc getMonthDay(int val) {
350         if (dooffset) {
351             val += offset;
352         }
353         Date JavaDoc v = new Date JavaDoc((long) val * 1000);
354         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc("dd");
355         return f.format(v);
356     }
357
358     /**
359      * Takes an integer representing the number of seconds from 1-Jan-1970 00:00:00 and returns the number of the month
360      *
361      * @param val Number of seconds from 1-Jan-1970 00:00:00
362      * @return String containing the number of the month (1 to 12)
363      * @see DateSupport#getTime
364      * @see DateSupport#getTimeSec
365      * @see DateSupport#getTimeSecLen
366      * @see DateSupport#getMonthDay
367      * @see DateSupport#getYear
368      * @see DateSupport#getMonthInt
369      * @see DateSupport#getWeekDayInt
370      * @see DateSupport#getDayInt
371      */

372     public static String JavaDoc getMonth(int val) {
373         if (dooffset) {
374             val += offset;
375         }
376         Date JavaDoc v = new Date JavaDoc((long) val * 1000);
377         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc("MM");
378         return f.format(v);
379     }
380
381     /**
382      * Takes an integer representing the number of seconds from 1-Jan-1970 00:00:00 and returns the year
383      *
384      * @param val Number of seconds from 1-Jan-1970 00:00:00
385      * @return String containing the year (1900 to ....)
386      * @see DateSupport#getTime
387      * @see DateSupport#getTimeSec
388      * @see DateSupport#getTimeSecLen
389      * @see DateSupport#getMonthDay
390      * @see DateSupport#getMonth
391      * @see DateSupport#getMonthInt
392      * @see DateSupport#getWeekDayInt
393      * @see DateSupport#getDayInt
394      */

395     public static String JavaDoc getYear(int val) {
396         //log.debug(val);
397
if (dooffset) {
398             val += offset;
399         }
400         Date JavaDoc v = new Date JavaDoc(((long) val) * 1000);
401         Calendar JavaDoc c = Calendar.getInstance();
402         c.setTime(v);
403
404         return Integer.toString(c.get(Calendar.YEAR));
405     }
406
407     /**
408      * Takes an integer representing the number of seconds from 1-Jan-1970 00:00:00 and returns the month as an integer
409      *
410      * @param val Number of seconds from 1-Jan-1970 00:00:00
411      * @return Integer containing the value of the month (1 to 12)
412      * @see DateSupport#getTime
413      * @see DateSupport#getTimeSec
414      * @see DateSupport#getTimeSecLen
415      * @see DateSupport#getMonthDay
416      * @see DateSupport#getMonth
417      * @see DateSupport#getYear
418      * @see DateSupport#getWeekDayInt
419      * @see DateSupport#getDayInt
420      */

421     public static int getMonthInt(int val) {
422         if (dooffset) {
423             val += offset;
424         }
425         Date JavaDoc v = new Date JavaDoc((long) val * 1000);
426         Calendar JavaDoc c = Calendar.getInstance();
427         c.setTime(v);
428         return c.get(Calendar.MONTH);
429     }
430
431     /**
432      * Takes an integer representing the number of seconds from 1-Jan-1970 00:00:00
433      * and returns the number of the day in the week as an integer
434      *
435      * @param val Number of seconds from 1-Jan-1970 00:00:00
436      * @return Integer containing the number of the day in the week (0 to 6)
437      * @see DateSupport#getTime
438      * @see DateSupport#getTimeSec
439      * @see DateSupport#getTimeSecLen
440      * @see DateSupport#getMonthDay
441      * @see DateSupport#getMonth
442      * @see DateSupport#getYear
443      * @see DateSupport#getMonthInt
444      * @see DateSupport#getDayInt
445      */

446     public static int getWeekDayInt(int val) {
447         if (dooffset) {
448             val += offset;
449         }
450         Date JavaDoc v = new Date JavaDoc((long) val * 1000);
451         Calendar JavaDoc c = Calendar.getInstance();
452         c.setTime(v);
453         return c.get(Calendar.DAY_OF_WEEK)-1;
454     }
455
456     /**
457      * Takes an integer representing the number of seconds from 1-Jan-1970 00:00:00
458      * and returns the number of the day in the month as an integer
459      *
460      * @param val Number of seconds from 1-Jan-1970 00:00:00
461      * @return Integer containing the number of the day in the month (1 to 31)
462      * @see DateSupport#getTime
463      * @see DateSupport#getTimeSec
464      * @see DateSupport#getTimeSecLen
465      * @see DateSupport#getMonthDay
466      * @see DateSupport#getMonth
467      * @see DateSupport#getYear
468      * @see DateSupport#getMonthInt
469      * @see DateSupport#getWeekDayInt
470      */

471     public static int getDayInt(int val) {
472         if (dooffset) {
473             val += offset;
474         }
475         Date JavaDoc v = new Date JavaDoc((long) val * 1000);
476         Calendar JavaDoc c = Calendar.getInstance();
477         c.setTime(v);
478         return c.get(Calendar.DAY_OF_MONTH);
479     }
480
481     /**
482      * Return the time-difference between our timezone and GMT
483      *
484      * @return Integer containing the number of milliseconds representing the time-difference between us and GMT
485      */

486     public static long getMilliOffset() {
487         if (!dooffset) {
488             // Do not worry about the code below, since it will never be called
489

490             TimeZone JavaDoc tz1, tz2;
491             int off1, off2;
492             Date JavaDoc d = new Date JavaDoc();
493
494             tz1 = TimeZone.getDefault(); // This is MET but they think it's the Middle East
495
tz2 = TimeZone.getTimeZone("ECT"); //Apparently we live there ?
496
off1 = tz1.getRawOffset();
497             off2 = tz2.getRawOffset();
498             if (tz1.inDaylightTime(d)) {
499                 off1 += (3600 * 1000); // Activate before sunday morning
500
}
501
502             if (tz2.inDaylightTime(d)) {
503                 off2 += (3600 * 1000);
504             }
505             return off1 - off2;
506         } else {
507             return (long) offset * 1000;
508         }
509     }
510
511     /**
512      * Return the current time in milliseconds (for the current-timezone!!)
513      *
514      * @return Integer containing the number of milliseconds representing the current time
515      */

516     public static long currentTimeMillis() {
517         return System.currentTimeMillis() - getMilliOffset();
518     }
519
520     /**
521      * Convert a string (like "12:42:15 1/2/97") to milliseconds from 1970
522      * The timezone used is 'GMT'
523      * @param date String which contains the date and time in the format "hour:minutes:sec day/month/year"
524      * @return the elapsed milliseconds since 1970 from this date
525      */

526     public static long convertDateToLong(String JavaDoc date) {
527         // Next line was the old code:
528
// return (convertStringToLong(date));
529
log.debug("Converting " + date);
530         Calendar JavaDoc cal = Calendar.getInstance();
531         TimeZone JavaDoc tz = TimeZone.getDefault();
532
533         cal.setTimeZone(tz);
534         cal = parseDate(cal, date);
535
536         Date JavaDoc d = cal.getTime();
537         long l = d.getTime();
538
539         return l;
540     }
541
542     /*
543      * ----- private functions used by convertDateToLong --------
544      */

545
546
547     /**
548      * @obsolete Do not use this method ever!!
549      */

550     private static long convertStringToLongWithTimeZone(String JavaDoc date, int hour, int minutes) {
551         // Set timezone
552
Calendar JavaDoc calendar = setTimeZone(hour, minutes);
553
554         // Now convert the datestring to calendardate
555
calendar = parseDate(calendar, date);
556
557         // calculate the milliseconds since 1970
558
Date JavaDoc mydate = calendar.getTime();
559
560         // return this calculation
561
return mydate.getTime();
562     }
563
564     /**
565      * @obsolete Do not use this method ever!!
566      */

567     private static Calendar JavaDoc setTimeZone(int hours, int minutes) {
568         log.warn("obsolete setTimeZone was used!!");
569
570         // get the supported ids for GMT-08:00 (Pacific Standard Time)
571
String JavaDoc[] ids = TimeZone.getAvailableIDs((hours * 60 + minutes) * 60 * 1000);
572
573         // if no ids were returned, something is wrong. get out.
574
if (ids.length == 0) {
575             log.error("Timezone is wrong...");
576             System.exit(0); /// XXX should be return null!
577
}
578         System.out.println("Current Time");
579
580         // create a Pacific Standard Time time zone
581
SimpleTimeZone JavaDoc pdt = new SimpleTimeZone JavaDoc((hours * 60 + minutes) * 60 * 1000, ids[0]);
582
583         // set up rules for daylight savings time
584
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
585         pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
586
587         // create a GregorianCalendar with the Pacific Daylight time zone
588
// and the current date and time
589
Calendar JavaDoc calendar = new GregorianCalendar JavaDoc(pdt);
590         return calendar;
591     }
592
593     /**
594      * Parse a string containing a date and put it in a calendar
595      * @param cal Calander object that is used for storing the parsed date
596      * @param date String in the form: hour:minute:second day/month/year
597      * @return Calander object representing the parsed date
598      * @see DateSupport parseDateRev
599      */

600     public static Calendar JavaDoc parseDate(Calendar JavaDoc cal, String JavaDoc date) {
601         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(date, "-\n\r:/ ");
602         String JavaDoc token = null;
603
604         cal.clear(Calendar.HOUR_OF_DAY);
605
606         token = tok.nextToken();
607         cal.set(Calendar.HOUR_OF_DAY, new Integer JavaDoc(token).intValue());
608         token = tok.nextToken();
609         cal.set(Calendar.MINUTE, new Integer JavaDoc(token).intValue());
610         token = tok.nextToken();
611         cal.set(Calendar.SECOND, new Integer JavaDoc(token).intValue());
612         token = tok.nextToken();
613         cal.set(Calendar.DAY_OF_MONTH, new Integer JavaDoc(token).intValue());
614         token = tok.nextToken();
615         cal.set(Calendar.MONTH, new Integer JavaDoc(token).intValue() - 1);
616         token = tok.nextToken();
617         cal.set(Calendar.YEAR, new Integer JavaDoc(token).intValue());
618         return (cal);
619     }
620
621     /**
622      * Parse a string containing a date and put it in a calendar, the string is in reversed order
623      * @param cal Calander object that is used for storing the parsed date
624      * @param date String in the form: year/month/day hour:minute:second
625      * @return Calander object representing the parsed date
626      * @see DateSupport parseDate
627      */

628     public static Calendar JavaDoc parseDateRev(Calendar JavaDoc cal, String JavaDoc date) {
629         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(date, "-\n\r:/ ");
630         String JavaDoc token = null;
631
632         cal.clear(Calendar.HOUR_OF_DAY);
633
634         token = tok.nextToken();
635         cal.set(Calendar.YEAR, new Integer JavaDoc(token).intValue());
636         token = tok.nextToken();
637         cal.set(Calendar.MONTH, new Integer JavaDoc(token).intValue() - 1);
638         token = tok.nextToken();
639         cal.set(Calendar.DAY_OF_MONTH, new Integer JavaDoc(token).intValue());
640         token = tok.nextToken();
641         cal.set(Calendar.HOUR_OF_DAY, new Integer JavaDoc(token).intValue());
642         token = tok.nextToken();
643         cal.set(Calendar.MINUTE, new Integer JavaDoc(token).intValue());
644         token = tok.nextToken();
645         cal.set(Calendar.SECOND, new Integer JavaDoc(token).intValue());
646         return (cal);
647     }
648
649     /**
650      * Return a string for a given date
651      * @param time Integer representing the time in seconds since 1-Jan-1970 00:00:00
652      * @return String in the form 'hhmmss day/month/year'
653      * @see DateSupport#date2day
654      * @see DateSupport#date2date
655      */

656     public static String JavaDoc date2string(int time) {
657         return getTimeSec(time) + " " + getMonthDay(time) + "/" + getMonth(time) + "/" + getYear(time);
658     }
659
660     /**
661      * Return a string for a given date
662      * @param time Integer representing the time in seconds since 1-Jan-1970 00:00:00
663      * @return String in the form 'year-month-day'
664      * @see DateSupport#date2string
665      * @see DateSupport#date2date
666      */

667     public static String JavaDoc date2day(int time) {
668         return getYear(time) + "-" + getMonth(time) + "-" + getMonthDay(time);
669     }
670
671     /**
672      * Return a string for a given date
673      * @param time Integer representing the time in seconds since 1-Jan-1970 00:00:00
674      * @return String in the form 'year-month-day hhmmss'
675      * @see DateSupport#date2string
676      * @see DateSupport#date2day
677      */

678     public static String JavaDoc date2date(int time) {
679         return getYear(time) + "-" + getMonth(time) + "-" + getMonthDay(time) + " " + getTimeSec(time);
680     }
681
682     /**
683      * Dump a date as string
684      * @param time Integer representing the time in seconds since 1-Jan-1970 00:00:00
685      * @return String with a date
686      */

687     private static String JavaDoc dumpdate(int d) {
688         Date JavaDoc dd = new Date JavaDoc((long) d * 1000);
689         return dd.toString();
690     }
691
692     /**
693      * Main method used for testing purposes
694      * @param args Array of arguments
695      */

696     public static void main(String JavaDoc args[]) {
697         log.info("Date (without corr)" + date2string((int) (System.currentTimeMillis() / 1000)) + " " + System.currentTimeMillis() / 1000);
698         log.info("Date (with corr)" + date2string((int) (DateSupport.currentTimeMillis() / 1000)) + " : " + DateSupport.currentTimeMillis() / 1000);
699         log.info("Date " + args[0] + " " + date2string(Integer.parseInt(args[0])));
700         log.info("Date " + args[0] + " " + dumpdate(Integer.parseInt(args[0])));
701         String JavaDoc ID = System.getProperty("user.timezone", "GMT");
702         log.info("ID " + ID + " : " + getMilliOffset());
703         log.info("ParseDate " + parsedate(args[1]));
704     }
705 }
706
Popular Tags