forked from thebiznatch/cloudhopper-commons-util
/
DateTimeUtil.java
299 lines (276 loc) · 11.7 KB
/
DateTimeUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* Copyright (C) 2011 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.cloudhopper.commons.util;
import java.sql.Timestamp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
/**
*
* @author joelauer (twitter: @jjlauer or <a href="http://twitter.com/jjlauer" target=window>http://twitter.com/jjlauer</a>)
*/
public class DateTimeUtil {
/**
* Null-safe method of converting a SQL Timestamp into a DateTime that
* it set specifically to be in UTC.
* <br>
* NOTE: The timestamp also should be in UTC.
* @return A UTC DateTime
*/
public static DateTime toDateTime(Timestamp value) {
if (value == null) {
return null;
} else {
return new DateTime(value.getTime(), DateTimeZone.UTC);
}
}
/**
* Null-safe method of copying a DateTime
* <br>
* NOTE: The timestamp also should be in UTC.
* @return A UTC DateTime
*/
public static DateTime copy(DateTime value) {
if (value == null) {
return null;
} else {
return new DateTime(value.getMillis(), DateTimeZone.UTC);
}
}
/**
* Null-safe method of converting a DateTime to a Timestamp.
* <br>
* NOTE: The timestamp also should be in UTC.
* @return A UTC DateTime
*/
public static Timestamp toTimestamp(DateTime dt) {
if (dt == null) {
return null;
} else {
return new Timestamp(dt.getMillis());
}
}
/**
* Get the current time as a DateTime.
*
* @return A UTC DateTime
*/
public static DateTime now() {
return new DateTime(DateTimeZone.UTC);
}
/**
* Parses a string for an embedded date and/or time contained within a
* string such as "app.2008-05-01.log". This method expects the string
* to contain a pattern of "yyyy-MM-dd". All dates will be interpreted to
* be in the UTC timezone.
* @param string0 The string to parse
* @return The parsed DateTime value
* @throws IllegalArgumentException Thrown if the string did not contain
* an embedded date.
*/
public static DateTime parseEmbedded(String string0) throws IllegalArgumentException {
return parseEmbedded(string0, "yyyy-MM-dd", DateTimeZone.UTC);
}
/**
* Parses a string for an embedded date and/or time contained within a
* string such as "app.2008-05-01.log" or "app-20090624-051112.log.gz".
* This method accepts a variety of date and time patterns that are valid
* within the Joda DateTime library. For example, the string "app.2008-05-01.log"
* would be a pattern of "yyyy-MM-dd" and the string "app-20090624-151112.log.gz"
* would be a pattern of "yyyy-MM-dd-HHmmss".
* @param string0 The string to parse
* @param pattern The DateTime pattern embedded in the string such as
* "yyyy-MM-dd". The pattern must be a valid DateTime Joda pattern.
* @param zone The timezone the parsed date will be in
* @return The parsed DateTime value
* @throws IllegalArgumentException Thrown if the pattern is invalid or if
* string did not contain an embedded date.
*/
public static DateTime parseEmbedded(String string0, String pattern, DateTimeZone zone) throws IllegalArgumentException {
// compile a date time formatter -- which also will check that pattern
DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern).withZone(zone);
//
// use the pattern to generate a regular expression now -- we'll do a
// simple replacement of ascii characters with \\d
//
StringBuilder regex = new StringBuilder(pattern.length()*2);
for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i);
if (Character.isLetter(c)) {
regex.append("\\d");
} else {
regex.append(c);
}
}
// extract the date from the string
Pattern p = Pattern.compile(regex.toString()); // Compiles regular expression into Pattern.
Matcher m = p.matcher(string0); // Creates Matcher with subject s and Pattern p.
if (!m.find()) {
// if we get here, then no valid grouping was found
throw new IllegalArgumentException("String '" + string0 + "' did not contain an embedded date [regexPattern='" + regex.toString() + "', datePattern='" + pattern + "']");
}
//logger.debug("Matching date: " + m.group());
// this group represents a string in the format YYYY-MM-DD
String dateString = m.group();
// parse the string and return the Date
return fmt.parseDateTime(dateString);
}
/**
* Null-safe method that returns a new instance of a DateTime object rounded
* downwards to the nearest year. Note that the nearest valid year is actually
* the first of that given year (Jan 1). The time zone of the returned DateTime
* instance will be the same as the argument. Similar to a floor() function
* on a float.<br>
* Examples:
* <ul>
* <li>null -> null
* <li>"2009-06-24 13:24:51.476 -8:00" -> "2009-01-01 00:00:00.000 -8:00"
* </ul>
* @param value The DateTime value to round downward
* @return Null if the argument is null or a new instance of the DateTime
* value rounded downwards to the nearest year.
*/
public static DateTime floorToYear(DateTime value) {
if (value == null) {
return null;
}
return new DateTime(value.getYear(), 1, 1, 0, 0, 0, 0, value.getZone());
}
/**
* Null-safe method that returns a new instance of a DateTime object rounded
* downwards to the nearest month. Note that the nearest valid month is actually
* the first of that given month (1st day of month). The time zone of the returned DateTime
* instance will be the same as the argument. Similar to a floor() function
* on a float.<br>
* Examples:
* <ul>
* <li>null -> null
* <li>"2009-06-24 13:24:51.476 -8:00" -> "2009-06-01 00:00:00.000 -8:00"
* </ul>
* @param value The DateTime value to round downward
* @return Null if the argument is null or a new instance of the DateTime
* value rounded downwards to the nearest month.
*/
public static DateTime floorToMonth(DateTime value) {
if (value == null) {
return null;
}
return new DateTime(value.getYear(), value.getMonthOfYear(), 1, 0, 0, 0, 0, value.getZone());
}
/**
* Null-safe method that returns a new instance of a DateTime object rounded
* downwards to the nearest day. The time zone of the returned DateTime
* instance will be the same as the argument. Similar to a floor() function
* on a float.<br>
* Examples:
* <ul>
* <li>null -> null
* <li>"2009-06-24 13:24:51.476 -8:00" -> "2009-06-24 00:00:00.000 -8:00"
* </ul>
* @param value The DateTime value to round downward
* @return Null if the argument is null or a new instance of the DateTime
* value rounded downwards to the nearest day.
*/
public static DateTime floorToDay(DateTime value) {
if (value == null) {
return null;
}
return new DateTime(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth(), 0, 0, 0, 0, value.getZone());
}
/**
* Null-safe method that returns a new instance of a DateTime object rounded
* downwards to the nearest hour. The time zone of the returned DateTime
* instance will be the same as the argument. Similar to a floor() function
* on a float.<br>
* Examples:
* <ul>
* <li>null -> null
* <li>"2009-06-24 13:24:51.476 -8:00" -> "2009-06-24 13:00:00.000 -8:00"
* </ul>
* @param value The DateTime value to round downward
* @return Null if the argument is null or a new instance of the DateTime
* value rounded downwards to the nearest hour.
*/
public static DateTime floorToHour(DateTime value) {
if (value == null) {
return null;
}
return new DateTime(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth(), value.getHourOfDay(), 0, 0, 0, value.getZone());
}
/**
* Null-safe method that returns a new instance of a DateTime object rounded
* downwards to the nearest five minutes. The time zone of the returned DateTime
* instance will be the same as the argument. Similar to a floor() function
* on a float.<br>
* Examples:
* <ul>
* <li>null -> null
* <li>"2009-06-24 13:24:51.476 -8:00" -> "2009-06-24 13:20:00.000 -8:00"
* </ul>
* @param value The DateTime value to round downward
* @return Null if the argument is null or a new instance of the DateTime
* value rounded downwards to the nearest five minutes.
*/
public static DateTime floorToFiveMinutes(DateTime value) {
if (value == null) {
return null;
}
int min = value.getMinuteOfHour();
min = (min / 5) * 5;
return new DateTime(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth(), value.getHourOfDay(), min, 0, 0, value.getZone());
}
/**
* Null-safe method that returns a new instance of a DateTime object rounded
* downwards to the nearest minute. The time zone of the returned DateTime
* instance will be the same as the argument. Similar to a floor() function
* on a float.<br>
* Examples:
* <ul>
* <li>null -> null
* <li>"2009-06-24 13:24:51.476 -8:00" -> "2009-06-24 13:24:00.000 -8:00"
* </ul>
* @param value The DateTime value to round downward
* @return Null if the argument is null or a new instance of the DateTime
* value rounded downwards to the nearest minute.
*/
public static DateTime floorToMinute(DateTime value) {
if (value == null) {
return null;
}
return new DateTime(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth(), value.getHourOfDay(), value.getMinuteOfHour(), 0, 0, value.getZone());
}
/**
* Null-safe method that returns a new instance of a DateTime object rounded
* downwards to the nearest second. The time zone of the returned DateTime
* instance will be the same as the argument. Similar to a floor() function
* on a float.<br>
* Examples:
* <ul>
* <li>null -> null
* <li>"2009-06-24 13:24:51.476 -8:00" -> "2009-06-24 13:24:51.000 -8:00"
* </ul>
* @param value The DateTime value to round downward
* @return Null if the argument is null or a new instance of the DateTime
* value rounded downwards to the nearest second.
*/
public static DateTime floorToSecond(DateTime value) {
if (value == null) {
return null;
}
return new DateTime(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth(), value.getHourOfDay(), value.getMinuteOfHour(), value.getSecondOfMinute(), 0, value.getZone());
}
}