forked from wujun728/jun_java_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtil.java
More file actions
276 lines (246 loc) · 6.6 KB
/
Copy pathDateUtil.java
File metadata and controls
276 lines (246 loc) · 6.6 KB
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
package org.myframework.util;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 封装日期处理相关方法
* 增加ORACLE中的ADD_MONTH ,SYSDATE 等类似功能的方法
* @author Administrator
*
*/
public class DateUtil {
private static Log log = LogFactory.getLog(DateUtil.class);
/** 采用Singleton设计模式而具有的唯一实例 */
private static DateUtil instance = new DateUtil();
private static String defaultPattern = "yyyy-MM-dd";
/** 格式化器存储器 */
private static Map<String, SimpleDateFormat> formats;
private DateUtil() {
resetFormats();
}
/**
* 通过缺省日期格式得到的工具类实例
*
* @return <code>DateUtilities</code>
*/
public static DateUtil getInstance() {
return instance;
}
/** Reset the supported formats to the default set. */
public void resetFormats() {
formats = new HashMap<String, SimpleDateFormat>();
// alternative formats
formats.put("yyyy-MM-dd HH:mm:ss", new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"));
formats.put("yyyyMMddHHmmssms", new SimpleDateFormat(
"yyyyMMddHHmmssms"));
// alternative formats
formats.put("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd"));
// XPDL examples format
formats.put("MM/dd/yyyy HH:mm:ss a", new SimpleDateFormat(
"MM/dd/yyyy HH:mm:ss a"));
// alternative formats
formats.put("yyyy-MM-dd HH:mm:ss a", new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss a"));
// ISO formats
formats.put("yyyy-MM-dd'T'HH:mm:ss'Z'", new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'Z'"));
formats.put("yyyy-MM-dd'T'HH:mm:ssZ", new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZ"));
formats.put("yyyy-MM-dd'T'HH:mm:ssz", new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssz"));
}
/**
* 格式化日期字符串
*
* @param date
* 日期
* @param pattern
* 日期格式
* @return
*/
public static String format(Date date, String pattern) {
if (!formats.containsKey(pattern))
pattern = defaultPattern;
DateFormat format = formats.get(pattern);
return format.format(date);
}
/**
* 自动判断格式化类型
*
* @param date
* 需格式化的日期
* @return 格式化后的字符串
*/
public static String format(Date date) {
Iterator<SimpleDateFormat> iter = formats.values().iterator();
while (iter.hasNext()) {
return iter.next().format(date);
}
return null;
}
/**
* 解析日期
*
* @param date
* 日期字符串
* @param pattern
* 日期格式
* @return
*/
public static Date parse(String date, String pattern) {
Date resultDate = null;
try {
if (!formats.containsKey(pattern))
pattern = defaultPattern;
resultDate = formats.get(pattern).parse(date);
} catch (ParseException e) {
log.error(e);
}
return resultDate;
}
/**
* 解析字符串到日期型
*
* @param dateString
* 日期字符串
* @return 返回日期型对象
*/
public static Date parse(String dateString) {
Iterator<SimpleDateFormat> iter = formats.values().iterator();
while (iter.hasNext()) {
try {
return iter.next().parse(dateString);
} catch (ParseException e) {
log.error(e);
}
}
return null;
}
/**
* 取得当前日期
*
* @return
*/
public static Timestamp getCurrentTimestamp() {
return new Timestamp(System.currentTimeMillis());
}
/**
* @param offsetYear
* @return 当前时间 + offsetYear
*/
public static Timestamp getTimestampExpiredYear(int offsetYear) {
Calendar now = Calendar.getInstance();
now.add(Calendar.YEAR, offsetYear);
return new Timestamp(now.getTime().getTime());
}
/**
* @param offsetMonth
* @return 当前时间 + offsetMonth
*/
public static Timestamp getCurrentTimestampExpiredMonth(int offsetMonth) {
Calendar now = Calendar.getInstance();
now.add(Calendar.MONTH, offsetMonth);
return new Timestamp(now.getTime().getTime());
}
/**
* @param offsetDay
* @return 当前时间 + offsetDay
*/
public static Timestamp getCurrentTimestampExpiredDay(int offsetDay) {
Calendar now = Calendar.getInstance();
now.add(Calendar.DATE, offsetDay);
return new Timestamp(now.getTime().getTime());
}
/**
* @param offsetSecond
* @return 当前时间 + offsetSecond
*/
public static Timestamp getCurrentTimestampExpiredSecond(int offsetSecond) {
Calendar now = Calendar.getInstance();
now.add(Calendar.SECOND, offsetSecond);
return new Timestamp(now.getTime().getTime());
}
/**
* @param offsetDay
* @return 指定时间 + offsetDay
*/
public static Timestamp getTimestampExpiredDay(Date givenDate, int offsetDay) {
Calendar date = Calendar.getInstance();
date.setTime(givenDate);
date.add(Calendar.DATE, offsetDay);
return new Timestamp(date.getTime().getTime());
}
/**
* 实现ORACLE中ADD_MONTHS函数功能
*
* @param offsetMonth
* @return 指定时间 + offsetMonth
*/
public static Timestamp getTimestampExpiredMonth(Date givenDate,
int offsetMonth) {
Calendar date = Calendar.getInstance();
date.setTime(givenDate);
date.add(Calendar.MONTH, offsetMonth);
return new Timestamp(date.getTime().getTime());
}
/**
* @param offsetSecond
* @return 指定时间 + offsetSecond
*/
public static Timestamp getTimestampExpiredSecond(Date givenDate,
int offsetSecond) {
Calendar date = Calendar.getInstance();
date.setTime(givenDate);
date.add(Calendar.SECOND, offsetSecond);
return new Timestamp(date.getTime().getTime());
}
/**
* @param offsetSecond
* @return 指定时间 + offsetSecond
*/
public static Timestamp getTimestampExpiredHour(Date givenDate,
int offsetHour) {
Calendar date = Calendar.getInstance();
date.setTime(givenDate);
date.add(Calendar.HOUR, offsetHour);
return new Timestamp(date.getTime().getTime());
}
/**
* @return 当前日期 yyyy-MM-dd
*/
public static String getCurrentDay() {
return format(new Date(), defaultPattern);
}
/**
* @return 当前的时间戳 yyyy-MM-dd HH:mm:ss
*/
public static String getNowTime() {
return format(new Date(),"yyyy-MM-dd HH:mm:ss");
}
/**
* @return 给出指定日期的月份的第一天
*/
public static Date getMonthFirstDay(Date givenDate) {
Date date = DateUtil.parse(DateUtil.format(givenDate, "yyyy-MM"),
"yyyy-MM");
return date;
}
/**
* @return 给出指定日期的月份的最后一天
*/
public static Date getMonthLastDay(Date givenDate) {
Date firstDay = getMonthFirstDay(givenDate);
Date lastMonthFirstDay = DateUtil.getTimestampExpiredMonth(firstDay, 1);
Date lastDay = DateUtil.getTimestampExpiredDay(lastMonthFirstDay, -1);
return lastDay;
}
}