1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.util; |
2 |
> |
package uk.org.iscream.cms.server.util; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import java.util.*; |
24 |
|
public static final long secsPerWeek = 7*24*60*60; |
25 |
|
public static final long secsPerDay = 24*60*60; |
26 |
|
public static final long secsPerHour = 60*60; |
27 |
+ |
public static final long secsPerMin = 60; |
28 |
|
|
29 |
+ |
private static final String daysKey = "%DAYS%"; |
30 |
+ |
private static final String hoursKey = "%HOURS%"; |
31 |
+ |
private static final String minsKey = "%MINS%"; |
32 |
+ |
private static final String secsKey = "%SECS%"; |
33 |
+ |
|
34 |
|
//---STATIC METHODS--- |
35 |
|
|
36 |
|
/** |
125 |
|
SimpleDateFormat formatter = new SimpleDateFormat("EEE"); |
126 |
|
Date conv = new Date(t*1000); |
127 |
|
return formatter.format(conv); |
128 |
< |
} |
129 |
< |
|
128 |
> |
} |
129 |
> |
|
130 |
|
/** |
131 |
|
* Return a String representation of the time (short format). |
132 |
|
*/ |
134 |
|
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); |
135 |
|
Date conv = new Date(t*1000); |
136 |
|
return formatter.format(conv); |
137 |
< |
} |
137 |
> |
} |
138 |
|
|
139 |
+ |
/** |
140 |
+ |
* Format a long time (in seconds) as a String. Can be used to |
141 |
+ |
* clearly layout a period of time in days, hours, minutes and |
142 |
+ |
* seconds. It could, for example, be used to format an uptime. |
143 |
+ |
* This method uses a built in default layout, as shown here; |
144 |
+ |
* "Days: xx, Hours: xx, Mins: xx, Secs: xx" |
145 |
+ |
* |
146 |
+ |
* @param time A long value representing the time period in seconds |
147 |
+ |
* @return A string representation of the given time period |
148 |
+ |
*/ |
149 |
+ |
public static String formatTime(long time) { |
150 |
+ |
String defaultLayout = "Days: "+daysKey+", Hours: "+hoursKey+", Mins: "+minsKey+", Secs: "+secsKey; |
151 |
+ |
return formatTime(time, defaultLayout); |
152 |
+ |
} |
153 |
+ |
|
154 |
+ |
/** |
155 |
+ |
* Format a long time (in seconds) as a String. Can be used to |
156 |
+ |
* clearly layout a period of time in days, hours, minutes and |
157 |
+ |
* seconds. It could, for example, be used to format an uptime. |
158 |
+ |
* This method uses a custom layout given as a String. This string |
159 |
+ |
* should contain the following keys; |
160 |
+ |
* "%DAYS% %HOURS% %MINS% %SECS%" |
161 |
+ |
* As an example, the default layout given by formatTime() is |
162 |
+ |
* represented by the following layout String; |
163 |
+ |
* "Days: %DAYS%, Hours: %HOURS%, Mins: %MINS%, Secs: %SECS%" |
164 |
+ |
* |
165 |
+ |
* @param time A long value representing the time period in seconds |
166 |
+ |
* @param layout The custom layout format to use. |
167 |
+ |
* @return A string representation of the given time period |
168 |
+ |
*/ |
169 |
+ |
public static String formatTime(long time, String layout) { |
170 |
+ |
// calculate days |
171 |
+ |
String days = new Long(time / secsPerDay).toString(); |
172 |
+ |
time = time % secsPerDay; |
173 |
+ |
|
174 |
+ |
// calculate hours |
175 |
+ |
String hours = new Long(time / secsPerHour).toString(); |
176 |
+ |
time = time % secsPerHour; |
177 |
+ |
|
178 |
+ |
// calculate minutes |
179 |
+ |
String mins = new Long(time / secsPerMin).toString(); |
180 |
+ |
time = time % secsPerMin; |
181 |
+ |
|
182 |
+ |
// seconds are left over |
183 |
+ |
String secs = new Long(time).toString(); |
184 |
+ |
|
185 |
+ |
// put the values into the layout |
186 |
+ |
layout = StringUtils.replaceText(layout, daysKey, days); |
187 |
+ |
layout = StringUtils.replaceText(layout, hoursKey, hours); |
188 |
+ |
layout = StringUtils.replaceText(layout, minsKey, mins); |
189 |
+ |
layout = StringUtils.replaceText(layout, secsKey, secs); |
190 |
+ |
|
191 |
+ |
return layout; |
192 |
+ |
} |
193 |
+ |
|
194 |
+ |
/** |
195 |
+ |
* Takes a time period in seconds and converts it to a |
196 |
+ |
* reasonable message. |
197 |
+ |
* |
198 |
+ |
* @param time the time period in seconds |
199 |
+ |
* @return a String respresentation of the given time period |
200 |
+ |
*/ |
201 |
+ |
public static String getTimeString(long time) { |
202 |
+ |
String timeString = null; |
203 |
+ |
if (time >= 3600) { |
204 |
+ |
timeString = ((time/60) / 60) + " hour(s)"; |
205 |
+ |
} else if (time >= 60) { |
206 |
+ |
timeString = (time / 60) + " minute(s)"; |
207 |
+ |
} else { |
208 |
+ |
timeString = time + " second(s)"; |
209 |
+ |
} |
210 |
+ |
return timeString; |
211 |
+ |
} |
212 |
|
|
213 |
|
//---CONSTRUCTORS--- |
214 |
|
|
218 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
219 |
|
* method to provide clean logging (every class should have this). |
220 |
|
* |
221 |
< |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
221 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
222 |
|
* to format the toString() |
223 |
|
* |
224 |
|
* @return the name of this class and its CVS revision |
229 |
|
getClass().getName(), |
230 |
|
REVISION); |
231 |
|
} |
232 |
< |
|
232 |
> |
|
233 |
|
//---PRIVATE METHODS--- |
234 |
|
|
235 |
|
//---ACCESSOR/MUTATOR METHODS--- |