1 package gboat2.base.bridge.util;
2
3 import java.text.ParseException;
4 import java.util.Calendar;
5 import java.util.Date;
6
7 import org.apache.commons.lang3.time.DateFormatUtils;
8 import org.apache.commons.lang3.time.DateUtils;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12
13
14
15
16
17
18
19 public abstract class DateUtil {
20
21 private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
22
23
24 public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
25
26
27 public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
28
29
30 public static final String ZH_DATETIME_FORMAT = "yyyy年M月d日 H时m分s秒";
31
32
33 public static final String ZH_DATE_FORMAT = "yyyy年M月d日";
34
35
36 public static final String RFC3339_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
37
38
39 public static final String SHORT_DATE_FORMAT = "yyMMdd";
40
41
42 public static final String YYYYMMDD_FORMAT = "yyyyMMdd";
43
44
45 public static final String SECOND_FORMAT = "yyyyMMddHHmmss";
46
47
48 public static final String YYMMDDHHMMSS = "yyMMddHHmmss";
49
50
51 public static final String MILLISECOND_FORMAT = "yyyyMMddHHmmssSSS";
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 public static final String[] SUPPORT_FORMATS = new String[] {
77 "yyyy-MM-dd HH:mm:ss.SSS",
78 "yyyy-MM-dd HH:mm:ss",
79 "yyyy-MM-dd HH:mm",
80 "yyyy-MM-dd",
81 "yyyy-MM-dd'T'HH:mm:ss.SSS",
82 "yyyy-MM-dd'T'HH:mm:ss",
83 "yyyy-MM-dd'T'HH:mm",
84 "yyyy/MM/dd HH:mm:ss.SSS",
85 "yyyy/MM/dd HH:mm:ss",
86 "yyyy/MM/dd HH:mm",
87 "yyyy/MM/dd",
88 "yyyy年MM月dd日 HH时mm分ss秒",
89 "yyyy年MM月dd日 HH时mm分",
90 "yyyy年MM月dd日",
91 "yyyyMMddHHmmssSSS",
92 "yyyyMMddHHmmss",
93 "yyyyMMddHHmm",
94 "yyyyMMdd"
95 };
96
97
98
99
100
101
102
103
104 public static String format(Date date) {
105 return (date == null) ? null : DateFormatUtils.format(date, DEFAULT_DATETIME_FORMAT);
106 }
107
108
109
110
111
112
113
114
115
116
117 public static String formatWithDefault(Date date) {
118 return format(date, DEFAULT_DATE_FORMAT, "无");
119 }
120
121
122
123
124
125
126
127
128
129
130 public static String format(Date date, String pattern, String defaultValue) {
131 return (date == null) ? defaultValue : DateFormatUtils.format(date, pattern);
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 public static Date parse(String date) {
155 return parse(date, DEFAULT_DATETIME_FORMAT, DEFAULT_DATE_FORMAT,
156 ZH_DATETIME_FORMAT, ZH_DATE_FORMAT, RFC3339_FORMAT,
157 SHORT_DATE_FORMAT, YYYYMMDD_FORMAT, SECOND_FORMAT,
158 YYMMDDHHMMSS, MILLISECOND_FORMAT);
159 }
160
161
162
163
164
165
166
167
168 public static Date parse(String date, String... format) {
169 Date dt = null;
170 try {
171 dt = DateUtils.parseDate(date, format);
172 } catch (ParseException e) {
173 logger.warn(e.toString());
174 }
175 return dt;
176 }
177
178
179
180
181
182
183
184 public static Date getFirstDayOfMonth() {
185 return getFirstDayOfMonth(Calendar.getInstance().getTime());
186 }
187
188
189
190
191
192
193
194
195 public static Date getFirstDayOfMonth(Date date) {
196 return DateUtils.truncate(date, Calendar.MONTH);
197 }
198
199
200
201
202
203
204
205 public static String getFirstDayOfMonthAsString() {
206 return getFirstDayOfMonthAsString(Calendar.getInstance().getTime());
207 }
208
209
210
211
212
213
214
215
216
217 public static String getFirstDayOfMonthAsString(Date date) {
218 return DateFormatUtils.format(getFirstDayOfMonth(date), DEFAULT_DATETIME_FORMAT);
219 }
220
221
222
223
224
225
226
227 public static Date getLastDayOfMonth() {
228 return getLastDayOfMonth(Calendar.getInstance().getTime());
229 }
230
231
232
233
234
235
236
237
238 public static Date getLastDayOfMonth(Date date) {
239 Calendar c = Calendar.getInstance();
240 c.setTime(date);
241
242
243 c = DateUtils.ceiling(c, Calendar.MONTH);
244
245 c.add(Calendar.MILLISECOND, -1);
246 return c.getTime();
247 }
248
249
250
251
252
253
254
255 public static String getLastDayOfMonthAsString() {
256 return getLastDayOfMonthAsString(Calendar.getInstance().getTime());
257 }
258
259
260
261
262
263
264
265
266
267 public static String getLastDayOfMonthAsString(Date date) {
268 return DateFormatUtils.format(getLastDayOfMonth(), DEFAULT_DATETIME_FORMAT);
269 }
270
271
272
273
274
275
276
277
278
279
280
281 public static int getDaysBetween(Date begin, Date end) {
282 if ((null != begin) && (null != end)) {
283 long dateFromTime = DateUtils.truncate(begin, Calendar.DATE).getTime();
284 long dateToTime = DateUtils.truncate(end, Calendar.DATE).getTime();
285 long dateRange = (dateToTime - dateFromTime) / DateUtils.MILLIS_PER_DAY;
286 return (int) dateRange;
287 }
288 return 0;
289 }
290
291
292
293
294
295
296
297
298 public static String dataToUpper(Date date) {
299 if (date == null)
300 return "";
301 Calendar ca = Calendar.getInstance();
302 ca.setTime(date);
303 int year = ca.get(Calendar.YEAR);
304 int month = ca.get(Calendar.MONTH) + 1;
305 int day = ca.get(Calendar.DAY_OF_MONTH);
306 return numToUpper(year) + "年" + monthToUppder(month) + "月" + dayToUppder(day) + "日";
307 }
308
309
310 private static String numToUpper(int num) {
311
312 String u[] = { "O", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
313 char[] str = String.valueOf(num).toCharArray();
314 String rstr = "";
315 for (int i = 0; i < str.length; i++) {
316 rstr = rstr + u[Integer.parseInt(str[i] + "")];
317 }
318 return rstr;
319 }
320
321
322 private static String monthToUppder(int month) {
323 if (month < 10) {
324 return numToUpper(month);
325 } else if (month == 10) {
326 return "十";
327 } else {
328 return "十" + numToUpper(month - 10);
329 }
330 }
331
332
333 private static String dayToUppder(int day) {
334 if (day < 20) {
335 return monthToUppder(day);
336 } else {
337 char[] str = String.valueOf(day).toCharArray();
338 if (str[1] == '0') {
339 return numToUpper(Integer.parseInt(str[0] + "")) + "十";
340 } else {
341 return numToUpper(Integer.parseInt(str[0] + "")) + "十"
342 + numToUpper(Integer.parseInt(str[1] + ""));
343 }
344 }
345 }
346 }