1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.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$ |
12 |
* @version $Id$ |
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.1 $"; |
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 |
|
28 |
//---STATIC METHODS--- |
29 |
|
30 |
/** |
31 |
* Return the number of seconds between the epoch and midnight earlier today. |
32 |
*/ |
33 |
public static long startOfToday(){ |
34 |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd"); |
35 |
Date midnight = new Date(); |
36 |
String then = formatter.format(midnight); |
37 |
|
38 |
ParsePosition pos = new ParsePosition(0); |
39 |
midnight = formatter.parse(then, pos); |
40 |
|
41 |
return midnight.getTime()/1000; |
42 |
} |
43 |
|
44 |
/** |
45 |
* Return the number of seconds between the epoch and now. |
46 |
*/ |
47 |
public static long now(){ |
48 |
Date now = new Date(); |
49 |
return now.getTime()/1000; |
50 |
} |
51 |
|
52 |
/** |
53 |
* Accept a long representing the number of seconds since the epoch. |
54 |
* An integer specifies the number of months to subtract. |
55 |
* Returns a long representing the number of seconds since the epoch. |
56 |
*/ |
57 |
public static long subtractMonths(long before, int months){ |
58 |
return before - months*secsPerMonth; |
59 |
} |
60 |
|
61 |
/** |
62 |
* Accept a long representing the number of seconds since the epoch. |
63 |
* An integer specifies the number of weeks to subtract. |
64 |
* Returns a long representing the number of seconds since the epoch. |
65 |
*/ |
66 |
public static long subtractWeeks(long before, int weeks){ |
67 |
return before - weeks*secsPerWeek; |
68 |
} |
69 |
|
70 |
/** |
71 |
* Accept a long representing the number of seconds since the epoch. |
72 |
* An integer specifies the number of days to subtract. |
73 |
* Returns a long representing the number of seconds since the epoch. |
74 |
*/ |
75 |
public static long subtractDays(long before, int days){ |
76 |
return before - days*secsPerDay; |
77 |
} |
78 |
|
79 |
/** |
80 |
* Accept a long representing the number of seconds since the epoch. |
81 |
* An integer specifies the number of hours to subtract. |
82 |
* Returns a long representing the number of seconds since the epoch. |
83 |
*/ |
84 |
public static long subtractHours(long before, int hours){ |
85 |
return before - hours*secsPerHour; |
86 |
} |
87 |
|
88 |
/** |
89 |
* Return a nice String representation of the date. |
90 |
*/ |
91 |
public static String dateString(long t){ |
92 |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
93 |
Date conv = new Date(t*1000); |
94 |
return formatter.format(conv); |
95 |
} |
96 |
|
97 |
/** |
98 |
* Return a nice short String representation of the date. |
99 |
*/ |
100 |
public static String shortDateString(long t){ |
101 |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
102 |
Date conv = new Date(t*1000); |
103 |
return formatter.format(conv); |
104 |
} |
105 |
|
106 |
/** |
107 |
* Return a String representation of the hour. |
108 |
*/ |
109 |
public static String hourString(long t){ |
110 |
SimpleDateFormat formatter = new SimpleDateFormat("H"); |
111 |
Date conv = new Date(t*1000); |
112 |
return formatter.format(conv); |
113 |
} |
114 |
|
115 |
/** |
116 |
* Return a String representation of the day name (short format). |
117 |
*/ |
118 |
public static String dayName(long t){ |
119 |
SimpleDateFormat formatter = new SimpleDateFormat("EEE"); |
120 |
Date conv = new Date(t*1000); |
121 |
return formatter.format(conv); |
122 |
} |
123 |
|
124 |
/** |
125 |
* Return a String representation of the time (short format). |
126 |
*/ |
127 |
public static String timeString(long t){ |
128 |
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); |
129 |
Date conv = new Date(t*1000); |
130 |
return formatter.format(conv); |
131 |
} |
132 |
|
133 |
|
134 |
//---CONSTRUCTORS--- |
135 |
|
136 |
//---PUBLIC METHODS--- |
137 |
|
138 |
/** |
139 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
140 |
* method to provide clean logging (every class should have this). |
141 |
* |
142 |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
143 |
* to format the toString() |
144 |
* |
145 |
* @return the name of this class and its CVS revision |
146 |
*/ |
147 |
public String toString() { |
148 |
return FormatName.getName( |
149 |
_name, |
150 |
getClass().getName(), |
151 |
REVISION); |
152 |
} |
153 |
|
154 |
//---PRIVATE METHODS--- |
155 |
|
156 |
//---ACCESSOR/MUTATOR METHODS--- |
157 |
|
158 |
//---ATTRIBUTES--- |
159 |
|
160 |
/** |
161 |
* This is the friendly identifier of the |
162 |
* component this class is running in. |
163 |
* eg, a Filter may be called "filter1", |
164 |
* If this class does not have an owning |
165 |
* component, a name from the configuration |
166 |
* can be placed here. This name could also |
167 |
* be changed to null for utility classes. |
168 |
*/ |
169 |
private String _name = null; |
170 |
|
171 |
//---STATIC ATTRIBUTES--- |
172 |
|
173 |
} |