1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.3 |
package uk.org.iscream.util; |
3 |
tdb |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import java.util.*; |
6 |
|
|
import java.text.*; |
7 |
|
|
|
8 |
|
|
/** |
9 |
|
|
* Provides easy to use date functions. |
10 |
|
|
* |
11 |
tdb |
1.2 |
* @author $Author: tdb1 $ |
12 |
tdb |
1.3 |
* @version $Id: DateUtils.java,v 1.2 2001/02/05 22:21:20 tdb1 Exp $ |
13 |
tdb |
1.1 |
*/ |
14 |
|
|
public class DateUtils { |
15 |
|
|
|
16 |
|
|
//---FINAL ATTRIBUTES--- |
17 |
|
|
|
18 |
|
|
/** |
19 |
|
|
* The current CVS revision of this class |
20 |
|
|
*/ |
21 |
tdb |
1.3 |
public final String REVISION = "$Revision: 1.2 $"; |
22 |
tdb |
1.1 |
|
23 |
|
|
public static final long secsPerMonth = 30*24*60*60; |
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 |
tdb |
1.2 |
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 |
tdb |
1.1 |
|
34 |
|
|
//---STATIC METHODS--- |
35 |
|
|
|
36 |
|
|
/** |
37 |
|
|
* Return the number of seconds between the epoch and midnight earlier today. |
38 |
|
|
*/ |
39 |
|
|
public static long startOfToday(){ |
40 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd"); |
41 |
|
|
Date midnight = new Date(); |
42 |
|
|
String then = formatter.format(midnight); |
43 |
|
|
|
44 |
|
|
ParsePosition pos = new ParsePosition(0); |
45 |
|
|
midnight = formatter.parse(then, pos); |
46 |
|
|
|
47 |
|
|
return midnight.getTime()/1000; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
/** |
51 |
|
|
* Return the number of seconds between the epoch and now. |
52 |
|
|
*/ |
53 |
|
|
public static long now(){ |
54 |
|
|
Date now = new Date(); |
55 |
|
|
return now.getTime()/1000; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
/** |
59 |
|
|
* Accept a long representing the number of seconds since the epoch. |
60 |
|
|
* An integer specifies the number of months to subtract. |
61 |
|
|
* Returns a long representing the number of seconds since the epoch. |
62 |
|
|
*/ |
63 |
|
|
public static long subtractMonths(long before, int months){ |
64 |
|
|
return before - months*secsPerMonth; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* Accept a long representing the number of seconds since the epoch. |
69 |
|
|
* An integer specifies the number of weeks to subtract. |
70 |
|
|
* Returns a long representing the number of seconds since the epoch. |
71 |
|
|
*/ |
72 |
|
|
public static long subtractWeeks(long before, int weeks){ |
73 |
|
|
return before - weeks*secsPerWeek; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
/** |
77 |
|
|
* Accept a long representing the number of seconds since the epoch. |
78 |
|
|
* An integer specifies the number of days to subtract. |
79 |
|
|
* Returns a long representing the number of seconds since the epoch. |
80 |
|
|
*/ |
81 |
|
|
public static long subtractDays(long before, int days){ |
82 |
|
|
return before - days*secsPerDay; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
/** |
86 |
|
|
* Accept a long representing the number of seconds since the epoch. |
87 |
|
|
* An integer specifies the number of hours to subtract. |
88 |
|
|
* Returns a long representing the number of seconds since the epoch. |
89 |
|
|
*/ |
90 |
|
|
public static long subtractHours(long before, int hours){ |
91 |
|
|
return before - hours*secsPerHour; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
/** |
95 |
|
|
* Return a nice String representation of the date. |
96 |
|
|
*/ |
97 |
|
|
public static String dateString(long t){ |
98 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
99 |
|
|
Date conv = new Date(t*1000); |
100 |
|
|
return formatter.format(conv); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* Return a nice short String representation of the date. |
105 |
|
|
*/ |
106 |
|
|
public static String shortDateString(long t){ |
107 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
108 |
|
|
Date conv = new Date(t*1000); |
109 |
|
|
return formatter.format(conv); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
/** |
113 |
|
|
* Return a String representation of the hour. |
114 |
|
|
*/ |
115 |
|
|
public static String hourString(long t){ |
116 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("H"); |
117 |
|
|
Date conv = new Date(t*1000); |
118 |
|
|
return formatter.format(conv); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
/** |
122 |
|
|
* Return a String representation of the day name (short format). |
123 |
|
|
*/ |
124 |
|
|
public static String dayName(long t){ |
125 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("EEE"); |
126 |
|
|
Date conv = new Date(t*1000); |
127 |
|
|
return formatter.format(conv); |
128 |
tdb |
1.2 |
} |
129 |
|
|
|
130 |
tdb |
1.1 |
/** |
131 |
|
|
* Return a String representation of the time (short format). |
132 |
|
|
*/ |
133 |
|
|
public static String timeString(long t){ |
134 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); |
135 |
|
|
Date conv = new Date(t*1000); |
136 |
|
|
return formatter.format(conv); |
137 |
tdb |
1.2 |
} |
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 |
tdb |
1.1 |
|
194 |
|
|
|
195 |
|
|
//---CONSTRUCTORS--- |
196 |
|
|
|
197 |
|
|
//---PUBLIC METHODS--- |
198 |
|
|
|
199 |
|
|
/** |
200 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
201 |
|
|
* method to provide clean logging (every class should have this). |
202 |
|
|
* |
203 |
tdb |
1.3 |
* This uses the uk.org.iscream.util.FormatName class |
204 |
tdb |
1.1 |
* to format the toString() |
205 |
|
|
* |
206 |
|
|
* @return the name of this class and its CVS revision |
207 |
|
|
*/ |
208 |
|
|
public String toString() { |
209 |
|
|
return FormatName.getName( |
210 |
|
|
_name, |
211 |
|
|
getClass().getName(), |
212 |
|
|
REVISION); |
213 |
|
|
} |
214 |
tdb |
1.2 |
|
215 |
tdb |
1.1 |
//---PRIVATE METHODS--- |
216 |
|
|
|
217 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
218 |
|
|
|
219 |
|
|
//---ATTRIBUTES--- |
220 |
|
|
|
221 |
|
|
/** |
222 |
|
|
* This is the friendly identifier of the |
223 |
|
|
* component this class is running in. |
224 |
|
|
* eg, a Filter may be called "filter1", |
225 |
|
|
* If this class does not have an owning |
226 |
|
|
* component, a name from the configuration |
227 |
|
|
* can be placed here. This name could also |
228 |
|
|
* be changed to null for utility classes. |
229 |
|
|
*/ |
230 |
|
|
private String _name = null; |
231 |
|
|
|
232 |
|
|
//---STATIC ATTRIBUTES--- |
233 |
|
|
|
234 |
|
|
} |