1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* Copyright (C) 2000-2002 i-scream |
4 |
+ |
* |
5 |
+ |
* This program is free software; you can redistribute it and/or |
6 |
+ |
* modify it under the terms of the GNU General Public License |
7 |
+ |
* as published by the Free Software Foundation; either version 2 |
8 |
+ |
* of the License, or (at your option) any later version. |
9 |
+ |
* |
10 |
+ |
* This program is distributed in the hope that it will be useful, |
11 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
+ |
* GNU General Public License for more details. |
14 |
+ |
* |
15 |
+ |
* You should have received a copy of the GNU General Public License |
16 |
+ |
* along with this program; if not, write to the Free Software |
17 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
+ |
*/ |
19 |
+ |
|
20 |
|
//---PACKAGE DECLARATION--- |
21 |
< |
package uk.ac.ukc.iscream.util; |
21 |
> |
package uk.org.iscream.cms.server.util; |
22 |
|
|
23 |
|
//---IMPORTS--- |
24 |
|
import java.util.*; |
43 |
|
public static final long secsPerWeek = 7*24*60*60; |
44 |
|
public static final long secsPerDay = 24*60*60; |
45 |
|
public static final long secsPerHour = 60*60; |
46 |
+ |
public static final long secsPerMin = 60; |
47 |
|
|
48 |
+ |
private static final String daysKey = "%DAYS%"; |
49 |
+ |
private static final String hoursKey = "%HOURS%"; |
50 |
+ |
private static final String minsKey = "%MINS%"; |
51 |
+ |
private static final String secsKey = "%SECS%"; |
52 |
+ |
|
53 |
|
//---STATIC METHODS--- |
54 |
|
|
55 |
|
/** |
144 |
|
SimpleDateFormat formatter = new SimpleDateFormat("EEE"); |
145 |
|
Date conv = new Date(t*1000); |
146 |
|
return formatter.format(conv); |
147 |
< |
} |
148 |
< |
|
147 |
> |
} |
148 |
> |
|
149 |
|
/** |
150 |
|
* Return a String representation of the time (short format). |
151 |
|
*/ |
153 |
|
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); |
154 |
|
Date conv = new Date(t*1000); |
155 |
|
return formatter.format(conv); |
156 |
< |
} |
156 |
> |
} |
157 |
|
|
158 |
+ |
/** |
159 |
+ |
* Format a long time (in seconds) as a String. Can be used to |
160 |
+ |
* clearly layout a period of time in days, hours, minutes and |
161 |
+ |
* seconds. It could, for example, be used to format an uptime. |
162 |
+ |
* This method uses a built in default layout, as shown here; |
163 |
+ |
* "Days: xx, Hours: xx, Mins: xx, Secs: xx" |
164 |
+ |
* |
165 |
+ |
* @param time A long value representing the time period in seconds |
166 |
+ |
* @return A string representation of the given time period |
167 |
+ |
*/ |
168 |
+ |
public static String formatTime(long time) { |
169 |
+ |
String defaultLayout = "Days: "+daysKey+", Hours: "+hoursKey+", Mins: "+minsKey+", Secs: "+secsKey; |
170 |
+ |
return formatTime(time, defaultLayout); |
171 |
+ |
} |
172 |
+ |
|
173 |
+ |
/** |
174 |
+ |
* Format a long time (in seconds) as a String. Can be used to |
175 |
+ |
* clearly layout a period of time in days, hours, minutes and |
176 |
+ |
* seconds. It could, for example, be used to format an uptime. |
177 |
+ |
* This method uses a custom layout given as a String. This string |
178 |
+ |
* should contain the following keys; |
179 |
+ |
* "%DAYS% %HOURS% %MINS% %SECS%" |
180 |
+ |
* As an example, the default layout given by formatTime() is |
181 |
+ |
* represented by the following layout String; |
182 |
+ |
* "Days: %DAYS%, Hours: %HOURS%, Mins: %MINS%, Secs: %SECS%" |
183 |
+ |
* |
184 |
+ |
* @param time A long value representing the time period in seconds |
185 |
+ |
* @param layout The custom layout format to use. |
186 |
+ |
* @return A string representation of the given time period |
187 |
+ |
*/ |
188 |
+ |
public static String formatTime(long time, String layout) { |
189 |
+ |
// calculate days |
190 |
+ |
String days = new Long(time / secsPerDay).toString(); |
191 |
+ |
time = time % secsPerDay; |
192 |
+ |
|
193 |
+ |
// calculate hours |
194 |
+ |
String hours = new Long(time / secsPerHour).toString(); |
195 |
+ |
time = time % secsPerHour; |
196 |
+ |
|
197 |
+ |
// calculate minutes |
198 |
+ |
String mins = new Long(time / secsPerMin).toString(); |
199 |
+ |
time = time % secsPerMin; |
200 |
+ |
|
201 |
+ |
// seconds are left over |
202 |
+ |
String secs = new Long(time).toString(); |
203 |
+ |
|
204 |
+ |
// put the values into the layout |
205 |
+ |
layout = StringUtils.replaceText(layout, daysKey, days); |
206 |
+ |
layout = StringUtils.replaceText(layout, hoursKey, hours); |
207 |
+ |
layout = StringUtils.replaceText(layout, minsKey, mins); |
208 |
+ |
layout = StringUtils.replaceText(layout, secsKey, secs); |
209 |
+ |
|
210 |
+ |
return layout; |
211 |
+ |
} |
212 |
+ |
|
213 |
+ |
/** |
214 |
+ |
* Takes a time period in seconds and converts it to a |
215 |
+ |
* reasonable message. |
216 |
+ |
* |
217 |
+ |
* @param time the time period in seconds |
218 |
+ |
* @return a String respresentation of the given time period |
219 |
+ |
*/ |
220 |
+ |
public static String getTimeString(long time) { |
221 |
+ |
String timeString = null; |
222 |
+ |
if (time >= 3600) { |
223 |
+ |
timeString = ((time/60) / 60) + " hour(s)"; |
224 |
+ |
} else if (time >= 60) { |
225 |
+ |
timeString = (time / 60) + " minute(s)"; |
226 |
+ |
} else { |
227 |
+ |
timeString = time + " second(s)"; |
228 |
+ |
} |
229 |
+ |
return timeString; |
230 |
+ |
} |
231 |
|
|
232 |
|
//---CONSTRUCTORS--- |
233 |
|
|
237 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
238 |
|
* method to provide clean logging (every class should have this). |
239 |
|
* |
240 |
< |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
240 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
241 |
|
* to format the toString() |
242 |
|
* |
243 |
|
* @return the name of this class and its CVS revision |
248 |
|
getClass().getName(), |
249 |
|
REVISION); |
250 |
|
} |
251 |
< |
|
251 |
> |
|
252 |
|
//---PRIVATE METHODS--- |
253 |
|
|
254 |
|
//---ACCESSOR/MUTATOR METHODS--- |