ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/DateUtils.java
Revision: 1.5
Committed: Tue May 29 17:02:35 2001 UTC (22 years, 11 months ago) by tdb
Branch: MAIN
Branch point for: SERVER_PIRCBOT
Changes since 1.4: +4 -4 lines
Log Message:
Major change in the java package naming. This has been held off for some time
now, but it really needed doing. The future packaging of all i-scream products
will be;

uk.org.iscream.<product>.<subpart>.*

In the case of the central monitoring system server this will be;

uk.org.iscream.cms.server.*

The whole server has been changed to follow this structure, and tested to a
smallish extent. Further changes in other parts of the CMS will follow.

File Contents

# Content
1 //---PACKAGE DECLARATION---
2 package uk.org.iscream.cms.server.util;
3
4 //---IMPORTS---
5 import java.util.*;
6 import java.text.*;
7
8 /**
9 * Provides easy to use date functions.
10 *
11 * @author $Author: tdb1 $
12 * @version $Id: DateUtils.java,v 1.4 2001/03/16 17:14:23 tdb1 Exp $
13 */
14 public class DateUtils {
15
16 //---FINAL ATTRIBUTES---
17
18 /**
19 * The current CVS revision of this class
20 */
21 public final String REVISION = "$Revision: 1.4 $";
22
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 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 /**
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 }
129
130 /**
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 }
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
215 //---PUBLIC METHODS---
216
217 /**
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.org.iscream.cms.server.util.FormatName class
222 * to format the toString()
223 *
224 * @return the name of this class and its CVS revision
225 */
226 public String toString() {
227 return FormatName.getName(
228 _name,
229 getClass().getName(),
230 REVISION);
231 }
232
233 //---PRIVATE METHODS---
234
235 //---ACCESSOR/MUTATOR METHODS---
236
237 //---ATTRIBUTES---
238
239 /**
240 * This is the friendly identifier of the
241 * component this class is running in.
242 * eg, a Filter may be called "filter1",
243 * If this class does not have an owning
244 * component, a name from the configuration
245 * can be placed here. This name could also
246 * be changed to null for utility classes.
247 */
248 private String _name = null;
249
250 //---STATIC ATTRIBUTES---
251
252 }