KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > servlets > TimeManagement


1 package edu.rice.rubis.servlets;
2
3 import java.util.GregorianCalendar JavaDoc;
4
5 /** This class provides additionnal functions that the GregorianCalendar
6  * class does not provide. It is mainly to compute time differences and
7  * display the date in a database understandable format.
8  */

9
10 public class TimeManagement
11 {
12
13   public TimeManagement()
14   {
15   }
16
17   /** Returns a string representation of the current date (when the method is called)
18    * conforming to the following database format : 'YYYY-MM-DD hh:mm:ss'
19    *
20    * @return current date in database format
21    *
22    */

23   public static String JavaDoc currentDateToString()
24   {
25     GregorianCalendar JavaDoc d = new GregorianCalendar JavaDoc();
26     return dateToString(d);
27   }
28
29   /** Returns a string representation of a date conforming to
30    * the following database format : 'YYYY-MM-DD hh:mm:ss'
31    *
32    * @return current date in database format
33    *
34    */

35   public static String JavaDoc dateToString(GregorianCalendar JavaDoc d)
36   {
37     String JavaDoc result;
38     int year, month, day, hour, minute, second;
39
40     year = d.get(d.YEAR);
41     month = d.get(d.MONTH) + 1;
42     day = d.get(d.DATE);
43     hour = d.get(d.HOUR_OF_DAY);
44     minute = d.get(d.MINUTE);
45     second = d.get(d.SECOND);
46     result =
47       year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
48     return result;
49   }
50
51   /** Returns a string representation of the time elapsed between startDate
52    * and endDate. Example of a returned string : "1 month 3 days 6 hours 33 minutes 4 seconds 234 milliseconds"
53    *
54    * @param startDate beginning date
55    * @param endDate ending date
56    *
57    * @return string containing the time difference up to the millisecond
58    *
59    */

60   public static String JavaDoc diffTime(
61     GregorianCalendar JavaDoc startDate,
62     GregorianCalendar JavaDoc endDate)
63   {
64     int year, month, day, hour, minute, second, millis;
65     String JavaDoc result = "";
66
67     year = endDate.get(endDate.YEAR) - startDate.get(startDate.YEAR);
68     month = endDate.get(endDate.MONTH) - startDate.get(startDate.MONTH);
69     day = endDate.get(endDate.DATE) - startDate.get(startDate.DATE);
70     hour =
71       endDate.get(endDate.HOUR_OF_DAY) - startDate.get(startDate.HOUR_OF_DAY);
72     minute = endDate.get(endDate.MINUTE) - startDate.get(startDate.MINUTE);
73     second = endDate.get(endDate.SECOND) - startDate.get(startDate.SECOND);
74     millis =
75       endDate.get(endDate.MILLISECOND) - startDate.get(startDate.MILLISECOND);
76
77     if (millis < 0)
78     {
79       millis = millis + 1000;
80       second = second - 1;
81     }
82     if (second < 0)
83     {
84       second = second + 60;
85       minute = minute - 1;
86     }
87     if (minute < 0)
88     {
89       minute = minute + 60;
90       hour = hour - 1;
91     }
92     if (hour < 0)
93     {
94       hour = hour + 24;
95       day = day - 1;
96     }
97     if (day < 0)
98     {
99       day = day + startDate.getActualMaximum(startDate.DAY_OF_MONTH);
100       // is the same as startDate.DATE
101
month = month - 1;
102     }
103     if (month < 0)
104     {
105       month = month + 12;
106       year = year - 1;
107     }
108
109     if (year > 0)
110       result = year + " year";
111     if (year > 1)
112       result = result + "s ";
113     else
114       result = result + " ";
115     if (month > 0)
116       result = result + month + " month";
117     if (month > 1)
118       result = result + "s ";
119     else
120       result = result + " ";
121     if (day > 0)
122       result = result + day + " day";
123     if (day > 1)
124       result = result + "s ";
125     else
126       result = result + " ";
127     if (hour > 0)
128       result = result + hour + " hour";
129     if (hour > 1)
130       result = result + "s ";
131     else
132       result = result + " ";
133     if (minute > 0)
134       result = result + minute + " minute";
135     if (minute > 1)
136       result = result + "s ";
137     else
138       result = result + " ";
139     if (second > 0)
140       result = result + second + " second";
141     if (second > 1)
142       result = result + "s ";
143     else
144       result = result + " ";
145     result = result + millis + " millisecond";
146     if (millis > 1)
147       result = result + "s ";
148     else
149       result = result + " ";
150     return result;
151   }
152
153   /** Compute a new GregorianCalendar from a beginning date and a duration in days.
154    *
155    * @param startDate beginning date
156    * @param durationInDays number of days to add to startDate.
157    *
158    * @return date corresponding to startDate+durationInDays
159    *
160    */

161   public static GregorianCalendar JavaDoc addDays(
162     GregorianCalendar JavaDoc startDate,
163     int durationInDays)
164   {
165     GregorianCalendar JavaDoc date = (GregorianCalendar JavaDoc) startDate.clone();
166     date.add(date.DATE, durationInDays);
167     return date;
168   }
169 }
170
Popular Tags