1 |
tdb |
1.6 |
/* |
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 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
21 |
tdb |
1.5 |
package uk.org.iscream.cms.server.util; |
22 |
tdb |
1.1 |
|
23 |
|
|
//---IMPORTS--- |
24 |
|
|
import java.util.*; |
25 |
|
|
import java.text.*; |
26 |
|
|
|
27 |
|
|
/** |
28 |
|
|
* Provides easy to use date functions. |
29 |
|
|
* |
30 |
tdb |
1.6 |
* @author $Author: tdb $ |
31 |
|
|
* @version $Id: DateUtils.java,v 1.5 2001/05/29 17:02:35 tdb Exp $ |
32 |
tdb |
1.1 |
*/ |
33 |
|
|
public class DateUtils { |
34 |
|
|
|
35 |
|
|
//---FINAL ATTRIBUTES--- |
36 |
|
|
|
37 |
|
|
/** |
38 |
|
|
* The current CVS revision of this class |
39 |
|
|
*/ |
40 |
tdb |
1.6 |
public final String REVISION = "$Revision: 1.5 $"; |
41 |
tdb |
1.1 |
|
42 |
|
|
public static final long secsPerMonth = 30*24*60*60; |
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 |
tdb |
1.2 |
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 |
tdb |
1.1 |
|
53 |
|
|
//---STATIC METHODS--- |
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* Return the number of seconds between the epoch and midnight earlier today. |
57 |
|
|
*/ |
58 |
|
|
public static long startOfToday(){ |
59 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd"); |
60 |
|
|
Date midnight = new Date(); |
61 |
|
|
String then = formatter.format(midnight); |
62 |
|
|
|
63 |
|
|
ParsePosition pos = new ParsePosition(0); |
64 |
|
|
midnight = formatter.parse(then, pos); |
65 |
|
|
|
66 |
|
|
return midnight.getTime()/1000; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
/** |
70 |
|
|
* Return the number of seconds between the epoch and now. |
71 |
|
|
*/ |
72 |
|
|
public static long now(){ |
73 |
|
|
Date now = new Date(); |
74 |
|
|
return now.getTime()/1000; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
/** |
78 |
|
|
* Accept a long representing the number of seconds since the epoch. |
79 |
|
|
* An integer specifies the number of months to subtract. |
80 |
|
|
* Returns a long representing the number of seconds since the epoch. |
81 |
|
|
*/ |
82 |
|
|
public static long subtractMonths(long before, int months){ |
83 |
|
|
return before - months*secsPerMonth; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
/** |
87 |
|
|
* Accept a long representing the number of seconds since the epoch. |
88 |
|
|
* An integer specifies the number of weeks to subtract. |
89 |
|
|
* Returns a long representing the number of seconds since the epoch. |
90 |
|
|
*/ |
91 |
|
|
public static long subtractWeeks(long before, int weeks){ |
92 |
|
|
return before - weeks*secsPerWeek; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
/** |
96 |
|
|
* Accept a long representing the number of seconds since the epoch. |
97 |
|
|
* An integer specifies the number of days to subtract. |
98 |
|
|
* Returns a long representing the number of seconds since the epoch. |
99 |
|
|
*/ |
100 |
|
|
public static long subtractDays(long before, int days){ |
101 |
|
|
return before - days*secsPerDay; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
/** |
105 |
|
|
* Accept a long representing the number of seconds since the epoch. |
106 |
|
|
* An integer specifies the number of hours to subtract. |
107 |
|
|
* Returns a long representing the number of seconds since the epoch. |
108 |
|
|
*/ |
109 |
|
|
public static long subtractHours(long before, int hours){ |
110 |
|
|
return before - hours*secsPerHour; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* Return a nice String representation of the date. |
115 |
|
|
*/ |
116 |
|
|
public static String dateString(long t){ |
117 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
118 |
|
|
Date conv = new Date(t*1000); |
119 |
|
|
return formatter.format(conv); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* Return a nice short String representation of the date. |
124 |
|
|
*/ |
125 |
|
|
public static String shortDateString(long t){ |
126 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
127 |
|
|
Date conv = new Date(t*1000); |
128 |
|
|
return formatter.format(conv); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
/** |
132 |
|
|
* Return a String representation of the hour. |
133 |
|
|
*/ |
134 |
|
|
public static String hourString(long t){ |
135 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("H"); |
136 |
|
|
Date conv = new Date(t*1000); |
137 |
|
|
return formatter.format(conv); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
/** |
141 |
|
|
* Return a String representation of the day name (short format). |
142 |
|
|
*/ |
143 |
|
|
public static String dayName(long t){ |
144 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("EEE"); |
145 |
|
|
Date conv = new Date(t*1000); |
146 |
|
|
return formatter.format(conv); |
147 |
tdb |
1.2 |
} |
148 |
|
|
|
149 |
tdb |
1.1 |
/** |
150 |
|
|
* Return a String representation of the time (short format). |
151 |
|
|
*/ |
152 |
|
|
public static String timeString(long t){ |
153 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); |
154 |
|
|
Date conv = new Date(t*1000); |
155 |
|
|
return formatter.format(conv); |
156 |
tdb |
1.2 |
} |
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 |
tdb |
1.1 |
|
213 |
tdb |
1.4 |
/** |
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 |
tdb |
1.1 |
|
232 |
|
|
//---CONSTRUCTORS--- |
233 |
|
|
|
234 |
|
|
//---PUBLIC METHODS--- |
235 |
|
|
|
236 |
|
|
/** |
237 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
238 |
|
|
* method to provide clean logging (every class should have this). |
239 |
|
|
* |
240 |
tdb |
1.5 |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
241 |
tdb |
1.1 |
* to format the toString() |
242 |
|
|
* |
243 |
|
|
* @return the name of this class and its CVS revision |
244 |
|
|
*/ |
245 |
|
|
public String toString() { |
246 |
|
|
return FormatName.getName( |
247 |
|
|
_name, |
248 |
|
|
getClass().getName(), |
249 |
|
|
REVISION); |
250 |
|
|
} |
251 |
tdb |
1.2 |
|
252 |
tdb |
1.1 |
//---PRIVATE METHODS--- |
253 |
|
|
|
254 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
255 |
|
|
|
256 |
|
|
//---ATTRIBUTES--- |
257 |
|
|
|
258 |
|
|
/** |
259 |
|
|
* This is the friendly identifier of the |
260 |
|
|
* component this class is running in. |
261 |
|
|
* eg, a Filter may be called "filter1", |
262 |
|
|
* If this class does not have an owning |
263 |
|
|
* component, a name from the configuration |
264 |
|
|
* can be placed here. This name could also |
265 |
|
|
* be changed to null for utility classes. |
266 |
|
|
*/ |
267 |
|
|
private String _name = null; |
268 |
|
|
|
269 |
|
|
//---STATIC ATTRIBUTES--- |
270 |
|
|
|
271 |
|
|
} |