KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * RUBBoS: Rice University Bulletin Board System.
3  * Copyright (C) 2001-2004 Rice University and French National Institute For
4  * Research In Computer Science And Control (INRIA).
5  * Contact: jmob@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package edu.rice.rubbos.servlets;
26
27 import java.util.GregorianCalendar JavaDoc;
28
29 /**
30  * This class provides additionnal functions that the GregorianCalendar class
31  * does not provide. It is mainly to compute time differences and display the
32  * date in a database understandable format.
33  */

34
35 public class TimeManagement
36 {
37
38   public TimeManagement()
39   {
40   }
41
42   /**
43    * Returns a string representation of the current date (when the method is
44    * called) conforming to the following database format : 'YYYY-MM-DD hh:mm:ss'
45    *
46    * @return current date in database format
47    */

48   public static String JavaDoc currentDateToString()
49   {
50     GregorianCalendar JavaDoc d = new GregorianCalendar JavaDoc();
51     return dateToString(d);
52   }
53
54   /**
55    * Returns a string representation of a date conforming to the following
56    * database format : 'YYYY-MM-DD hh:mm:ss'
57    *
58    * @return current date in database format
59    */

60   public static String JavaDoc dateToString(GregorianCalendar JavaDoc d)
61   {
62     String JavaDoc result;
63     int year, month, day, hour, minute, second;
64
65     year = d.get(d.YEAR);
66     month = d.get(d.MONTH) + 1;
67     day = d.get(d.DATE);
68     hour = d.get(d.HOUR_OF_DAY);
69     minute = d.get(d.MINUTE);
70     second = d.get(d.SECOND);
71     result = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":"
72         + second;
73     return result;
74   }
75
76   /**
77    * Returns a string representation of the time elapsed between startDate and
78    * endDate. Example of a returned string : "1 month 3 days 6 hours 33 minutes
79    * 4 seconds 234 milliseconds"
80    *
81    * @param startDate beginning date
82    * @param endDate ending date
83    * @return string containing the time difference up to the millisecond
84    */

85   public static String JavaDoc diffTime(GregorianCalendar JavaDoc startDate,
86       GregorianCalendar JavaDoc endDate)
87   {
88     int year, month, day, hour, minute, second, millis;
89     String JavaDoc result = "";
90
91     year = endDate.get(endDate.YEAR) - startDate.get(startDate.YEAR);
92     month = endDate.get(endDate.MONTH) - startDate.get(startDate.MONTH);
93     day = endDate.get(endDate.DATE) - startDate.get(startDate.DATE);
94     hour = endDate.get(endDate.HOUR_OF_DAY)
95         - startDate.get(startDate.HOUR_OF_DAY);
96     minute = endDate.get(endDate.MINUTE) - startDate.get(startDate.MINUTE);
97     second = endDate.get(endDate.SECOND) - startDate.get(startDate.SECOND);
98     millis = endDate.get(endDate.MILLISECOND)
99         - startDate.get(startDate.MILLISECOND);
100
101     if (millis < 0)
102     {
103       millis = millis + 1000;
104       second = second - 1;
105     }
106     if (second < 0)
107     {
108       second = second + 60;
109       minute = minute - 1;
110     }
111     if (minute < 0)
112     {
113       minute = minute + 60;
114       hour = hour - 1;
115     }
116     if (hour < 0)
117     {
118       hour = hour + 24;
119       day = day - 1;
120     }
121     if (day < 0)
122     {
123       day = day + startDate.getActualMaximum(startDate.DAY_OF_MONTH); // is the
124
// same as
125
// startDate.DATE
126
month = month - 1;
127     }
128     if (month < 0)
129     {
130       month = month + 12;
131       year = year - 1;
132     }
133
134     if (year > 0)
135       result = year + " year";
136     if (year > 1)
137       result = result + "s ";
138     else
139       result = result + " ";
140     if (month > 0)
141       result = result + month + " month";
142     if (month > 1)
143       result = result + "s ";
144     else
145       result = result + " ";
146     if (day > 0)
147       result = result + day + " day";
148     if (day > 1)
149       result = result + "s ";
150     else
151       result = result + " ";
152     if (hour > 0)
153       result = result + hour + " hour";
154     if (hour > 1)
155       result = result + "s ";
156     else
157       result = result + " ";
158     if (minute > 0)
159       result = result + minute + " minute";
160     if (minute > 1)
161       result = result + "s ";
162     else
163       result = result + " ";
164     if (second > 0)
165       result = result + second + " second";
166     if (second > 1)
167       result = result + "s ";
168     else
169       result = result + " ";
170     result = result + millis + " millisecond";
171     if (millis > 1)
172       result = result + "s ";
173     else
174       result = result + " ";
175     return result;
176   }
177
178   /**
179    * Compute a new GregorianCalendar from a beginning date and a duration in
180    * days.
181    *
182    * @param startDate beginning date
183    * @param durationInDays number of days to add to startDate.
184    * @return date corresponding to startDate+durationInDays
185    */

186   public static GregorianCalendar JavaDoc addDays(GregorianCalendar JavaDoc startDate,
187       int durationInDays)
188   {
189     GregorianCalendar JavaDoc date = (GregorianCalendar JavaDoc) startDate.clone();
190     date.add(date.DATE, durationInDays);
191     return date;
192   }
193 }
Popular Tags