KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubbos > beans > TimeManagement


1 package edu.rice.rubbos.beans;
2
3 import java.util.GregorianCalendar JavaDoc;
4 import java.lang.String JavaDoc;
5
6 /** This class provides additionnal functions that the GregorianCalendar
7  * class does not provide. It is mainly to compute time differences and
8  * display the date in a database understandable format.
9  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
10  * @version 1.0
11  */

12
13 public class TimeManagement extends GregorianCalendar JavaDoc
14 {
15
16   /**
17    * Creates a new <code>TimeManagement</code> instance.
18    *
19    */

20   public TimeManagement()
21   {
22   }
23
24
25   /** Returns a string representation of the current date (when the method is called)
26    * conforming to the following database format : 'YYYY-MM-DD hh:mm:ss'
27    *
28    * @return current date in database format
29    *
30    */

31   public static String JavaDoc currentDateToString()
32   {
33     GregorianCalendar JavaDoc d = new GregorianCalendar JavaDoc();
34     return dateToString(d);
35   }
36
37
38   /**
39    * Returns a string representation of a date conforming to
40    * the following database format : 'YYYY-MM-DD hh:mm:ss'
41    *
42    * @param d a <code>GregorianCalendar</code> value
43    * @return current date in database format
44    *
45    */

46   public static String JavaDoc dateToString(GregorianCalendar JavaDoc d)
47   {
48     String JavaDoc result;
49     int year, month, day, hour, minute, second;
50
51     year = d.get(d.YEAR);
52     month = d.get(d.MONTH) + 1;
53     day = d.get(d.DATE);
54     hour = d.get(d.HOUR_OF_DAY);
55     minute = d.get(d.MINUTE);
56     second = d.get(d.SECOND);
57     result = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
58     return result;
59   }
60
61
62   /** Returns the time elapsed between startDate and endDate in milliseconds
63    *
64    * @param startDate beginning date
65    * @param endDate ending date
66    *
67    * @return elapsed time in milliseconds
68    *
69    */

70   public static long diffTimeInMs(GregorianCalendar JavaDoc startDate, GregorianCalendar JavaDoc endDate)
71   {
72     long year, month, day, hour, minute, second, millis;
73
74     year = endDate.get(endDate.YEAR)-startDate.get(startDate.YEAR);
75     month = endDate.get(endDate.MONTH)-startDate.get(startDate.MONTH);
76     day = endDate.get(endDate.DATE)-startDate.get(startDate.DATE);
77     hour = endDate.get(endDate.HOUR_OF_DAY)-startDate.get(startDate.HOUR_OF_DAY);
78     minute = endDate.get(endDate.MINUTE)-startDate.get(startDate.MINUTE);
79     second = endDate.get(endDate.SECOND)-startDate.get(startDate.SECOND);
80     millis = endDate.get(endDate.MILLISECOND)-startDate.get(startDate.MILLISECOND);
81  
82     if (millis < 0)
83     {
84       millis = millis + 1000;
85       second = second - 1;
86     }
87     if (second < 0)
88     {
89       second = second + 60;
90       minute = minute - 1;
91     }
92     if (minute < 0)
93     {
94       minute = minute + 60;
95       hour = hour - 1;
96     }
97     if (hour < 0)
98     {
99       hour = hour + 24;
100       day = day - 1;
101     }
102     if (day < 0)
103     {
104       day = day + startDate.getActualMaximum(startDate.DAY_OF_MONTH); // is the same as startDate.DATE
105
month = month - 1;
106     }
107     if (month < 0)
108     {
109       month = month + 12;
110       year = year - 1;
111     }
112     return millis+(second*1000)+(minute*60000)+(hour*3600000)+(day*24*3600000)+(month*30*24*3600000); // We approximate 30 days per month
113
}
114
115
116   /** Returns a string representation of the time elapsed between startDate
117    * and endDate. Example of a returned string : "1 month 3 days 6 hours 33 minutes 4 seconds 234 milliseconds"
118    *
119    * @param startDate beginning date
120    * @param endDate ending date
121    *
122    * @return string containing the time difference up to the millisecond
123    *
124    */

125   public static String JavaDoc diffTime(GregorianCalendar JavaDoc startDate, GregorianCalendar JavaDoc endDate)
126   {
127     int year, month, day, hour, minute, second, millis;
128     String JavaDoc result = "";
129
130     year = endDate.get(endDate.YEAR)-startDate.get(startDate.YEAR);
131     month = endDate.get(endDate.MONTH)-startDate.get(startDate.MONTH);
132     day = endDate.get(endDate.DATE)-startDate.get(startDate.DATE);
133     hour = endDate.get(endDate.HOUR_OF_DAY)-startDate.get(startDate.HOUR_OF_DAY);
134     minute = endDate.get(endDate.MINUTE)-startDate.get(startDate.MINUTE);
135     second = endDate.get(endDate.SECOND)-startDate.get(startDate.SECOND);
136     millis = endDate.get(endDate.MILLISECOND)-startDate.get(startDate.MILLISECOND);
137  
138     if (millis < 0)
139     {
140       millis = millis + 1000;
141       second = second - 1;
142     }
143     if (second < 0)
144     {
145       second = second + 60;
146       minute = minute - 1;
147     }
148     if (minute < 0)
149     {
150       minute = minute + 60;
151       hour = hour - 1;
152     }
153     if (hour < 0)
154     {
155       hour = hour + 24;
156       day = day - 1;
157     }
158     if (day < 0)
159     {
160       day = day + startDate.getActualMaximum(startDate.DAY_OF_MONTH); // is the same as startDate.DATE
161
month = month - 1;
162     }
163     if (month < 0)
164     {
165       month = month + 12;
166       year = year - 1;
167     }
168
169     if (year > 0)
170       result = year+" year";
171     if (year > 1)
172       result = result+"s ";
173     else
174       result = result+" ";
175     if (month > 0)
176       result = result+month+" month";
177     if (month > 1)
178       result = result+"s ";
179     else
180       result = result+" ";
181     if (day > 0)
182       result = result+day+" day";
183     if (day > 1)
184       result = result+"s ";
185     else
186       result = result+" ";
187     if (hour > 0)
188       result = result+hour+" hour";
189     if (hour > 1)
190       result = result+"s ";
191     else
192       result = result+" ";
193     if (minute > 0)
194       result = result+minute+" minute";
195     if (minute > 1)
196       result = result+"s ";
197     else
198       result = result+" ";
199     if (second > 0)
200       result = result+second+" second";
201     if (second > 1)
202       result = result+"s ";
203     else
204       result = result+" ";
205     result = result+millis+" millisecond";
206     if (millis > 1)
207       result = result+"s ";
208     else
209       result = result+" ";
210     return result;
211   }
212
213
214   /** Compute a new GregorianCalendar from a beginning date and a duration in days.
215    *
216    * @param startDate beginning date
217    * @param durationInDays number of days to add to startDate.
218    *
219    * @return date corresponding to startDate+durationInDays
220    *
221    */

222   public static GregorianCalendar JavaDoc addDays(GregorianCalendar JavaDoc startDate, int durationInDays)
223   {
224     GregorianCalendar JavaDoc date = (GregorianCalendar JavaDoc)startDate.clone();
225     date.add(date.DATE, durationInDays);
226     return date;
227   }
228 }
229
Popular Tags