DateUtil.java
8.7 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package cn.com.polysoft.utils;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
*
*
* 日期转换处理
*/
public class DateUtil {
public static final String C_DATE_DIVISION = "-";
public static final String C_TIME_PATTON_DEFAULT = "yyyy-MM-dd HH:mm:ss";
public static final String C_DATE_PATTON_DEFAULT = "yyyy-MM-dd";
public static final String C_TIMES_PATTON_DEFAULT = "HH:mm:ss";
/**
* 返回当前时间
* @return
*/
public static Date getCurrentDate() {
Calendar cal = Calendar.getInstance();
Date currDate = cal.getTime();
return currDate;
}
/**
* 返回当前日期(String)
* @return
*/
public static String getCurrentDateStr() {
Calendar cal = Calendar.getInstance();
Date currDate = cal.getTime();
return format(currDate,C_DATE_PATTON_DEFAULT);
}
/**
* 返回当前时间(String)
* @return
*/
public static String getCurrentTimeStr() {
Calendar cal = Calendar.getInstance();
Date currDate = cal.getTime();
return format(currDate,C_TIMES_PATTON_DEFAULT);
}
/**
* 格式化日期字符串
* @param dateValue
* @return
*/
public static Date parseDate(String dateValue) {
return parseDate(C_DATE_PATTON_DEFAULT, dateValue);
}
/**
* 格式化时间字符串
* @param dateValue
* @return
*/
public static Date parseDateTime(String dateValue) {
return parseDate(C_TIME_PATTON_DEFAULT, dateValue);
}
/**
* 时间格式化
* @param strFormat
* @param dateValue
* @return
*/
public static Date parseDate(String strFormat, String dateValue) {
if (dateValue == null)
return null;
if (strFormat == null)
strFormat = C_TIME_PATTON_DEFAULT;
SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
Date newDate = null;
try {
newDate = dateFormat.parse(dateValue);
} catch (ParseException pe) {
newDate = null;
}
return newDate;
}
/**
* 将Timestamp类型的日期转换为系统参数定义的格式的字符串(yyyy-MM-dd)
*
* @param timestamp 需要转换的日期。
* @return 转换后符合给定格式的日期字符串
*/
public static String format(Date timestamp) {
return format(timestamp, C_DATE_PATTON_DEFAULT);
}
/**
* 将Timestamp类型的日期转换为系统参数定义的格式的字符串(yyyy-MM-dd HH:mm:ss)
*
* @param timestamp 需要转换的日期。
* @return 转换后符合给定格式的日期字符串
*/
public static String formatTime(Date timestamp) {
return format(timestamp, C_TIME_PATTON_DEFAULT);
}
/**
* 将Date类型的日期转换为系统参数定义的格式的字符串。
*
* @param timestamp
* @param pattern
* @return
*/
public static String format(Date timestamp, String pattern) {
if (timestamp == null || pattern == null)
return null;
SimpleDateFormat dateFromat = new SimpleDateFormat();
dateFromat.applyPattern(pattern);
return dateFromat.format(timestamp);
}
/**
* 取得格式化时间字符串(指定format)
* @param aTs_Datetime
* @param as_Format
* @return
*/
public static String formatTime(Date aTs_Datetime, String as_Format) {
if (aTs_Datetime == null || as_Format == null)
return null;
SimpleDateFormat dateFromat = new SimpleDateFormat();
dateFromat.applyPattern(as_Format);
return dateFromat.format(aTs_Datetime);
}
/**
* 取得格式化时间字符串(指定pattern)
* @return
*/
public static String format(Timestamp dateTime, String pattern) {
if (dateTime == null || pattern == null)
return null;
SimpleDateFormat dateFromat = new SimpleDateFormat();
dateFromat.applyPattern(pattern);
return dateFromat.format(dateTime);
}
/**
* 取得指定日期N天后的日期
*
* @param date
* @param days
* @return
*/
public static Date addDays(Date date, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, days);
return cal.getTime();
}
/**
* 取得指定日期N天前的日期
*
* @param days
* @return
*/
public static String beforeDays(String dateString, int days) {
String dateBefore = "";
Integer day = Integer.parseInt(dateString.substring(8));
if(day > days){
dateBefore= dateString.substring(0,4) + C_DATE_DIVISION +dateString.substring(5,7) +C_DATE_DIVISION+ (day - days);
}else{
dateBefore = dateString.substring(0,4) + C_DATE_DIVISION + (Integer.parseInt(dateString.substring(5,7))-1)+ C_DATE_DIVISION + day;
}
return dateBefore;
}
/**
* 计算两个日期之间相差的天数
*
* @param date1
* @param date2
* @return
*/
public static int daysBetween(Date date1, Date date2) {
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
long time1 = cal.getTimeInMillis();
cal.setTime(date2);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 计算当前日期相对于"1977-12-01"的天数
*
* @param date
* @return
*/
public static long getRelativeDays(Date date) {
Date relativeDate = DateUtil.parseDate("yyyy-MM-dd", "1977-12-01");
return DateUtil.daysBetween(relativeDate, date);
}
/**
* 传入时间字符串,加一天后返回Date
*
* @param date 时间 格式 YYYY-MM-DD
* @return
*/
public static Date addDate(String date) {
if (date == null) {
return null;
}
Date tempDate = parseDate(C_DATE_PATTON_DEFAULT, date);
String year = format(tempDate, "yyyy");
String month = format(tempDate, "MM");
String day = format(tempDate, "dd");
GregorianCalendar calendar = new GregorianCalendar(Integer
.parseInt(year), Integer.parseInt(month) - 1, Integer
.parseInt(day));
calendar.add(Calendar.DATE, 1);
return calendar.getTime();
}
/**
* 取得12月前的日期
* @return
*/
public static Date getDateBeforTwelveMonth() {
String date = "";
Calendar cla = Calendar.getInstance();
cla.setTime(getCurrentDate());
int year = cla.get(Calendar.YEAR) - 1;
int month = cla.get(Calendar.MONTH) + 1;
if (month > 9) {
date = String.valueOf(year) + C_DATE_DIVISION
+ String.valueOf(month) + C_DATE_DIVISION + "01";
} else {
date = String.valueOf(year) + C_DATE_DIVISION + "0"
+ String.valueOf(month) + C_DATE_DIVISION + "01";
}
Date dateBefore = parseDate(date);
return dateBefore;
}
/**
* 取得指定日期所在周开始
* @param date
* @return
*/
public static String getWeekStartDate(String date){
Calendar cal =Calendar.getInstance();
if (date!=null) {
cal.setTime(DateUtil.parseDate(date));
}
cal.add(Calendar.WEEK_OF_YEAR, 0);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String theWeekStartDate = format(cal.getTime());
cal.clear();
return theWeekStartDate;
}
/**
* 取得指定日期所在周结束
* @param date
* @return
*/
public static String getWeekEndDate(String date){
Calendar cal =Calendar.getInstance();
if (date!=null) {
cal.setTime(DateUtil.parseDate(date));
}
cal.add(Calendar.WEEK_OF_YEAR, 0);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
String theWeekStartDate = format(cal.getTime());
cal.clear();
return theWeekStartDate;
}
/**
* 取得指定日期所在月开始
* @param date
* @return
*/
public static String getMonthStartDate(String date){
Calendar cal =Calendar.getInstance();
if (date!=null&&date.trim().length()!=0) {
cal.setTime(DateUtil.parseDate(date));
}
cal.add(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH,1 );
String theWeekStartDate = format(cal.getTime(), C_DATE_PATTON_DEFAULT);
cal.clear();
return theWeekStartDate;
}
/**
* 取得指定日期所在月开始
* @param date
* @return
*/
public static String getMonthEndDate(String date){
Calendar cal =Calendar.getInstance();
if (date!=null&&date.trim().length()!=0) {
cal.setTime(DateUtil.parseDate(date));
}
cal.add(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
String theWeekStartDate = format(cal.getTime(), C_DATE_PATTON_DEFAULT);
cal.clear();
return theWeekStartDate;
}
/**
* 得到两个日期之间相差的年数
*
* @param date1
* @param date2
* @author mowei
*/
public static int getDifferYear(Date date1, Date date2) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date1);
c2.setTime(date2);
return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
}
public static void main(String[]args){
System.out.println(getDifferYear(new Date(),parseDate("2000-12-31")));
}
}