1 import java.text.ParseException;
2 import java.text.SimpleDateFormat;
3 import java.util.Calendar;
4 import java.util.Date;
5
6 import org.apache.commons.lang.StringUtils;
7
8 /**
9 * 函数功能
10 * 1.日期相加减天数
11 * 2.时间格式化成字符串
12 * 3.字符串解析成时间对象
13 * 4.将日期时间格式成只有日期的字符串(可以直接使用dateFormat,Pattern为Null进行格式化)
14 * 5.当时、分、秒为00:00:00时,将日期时间格式成只有日期的字符串
15 * 6.将日期时间格式成日期对象,和dateParse互用
16 * 7.时间加减小时
17 * 8.时间加减分钟
18 * 9.时间加减秒数
19 * 10.时间加减天数
20 * 11.时间加减月数
21 * 12.时间加减年数
22 * 13.时间比较(如果myDate>compareDate返回1,<返回-1,相等返回0)
23 * 14.获取两个时间中最小的一个时间
24 * 15.获取两个时间中最大的一个时间
25 * 16.获取两个日期(不含时分秒)相差的天数,不包含今天
26 * 17.获取两个日期(不含时分秒)相差的天数,包含今天
27 * 18.获取日期时间的年份,如2017-02-13,返回2017
28 * 19.获取日期时间的月份,如2017年2月13日,返回2
29 * 20.获取日期时间的第几天(即返回日期的dd),如2017-02-13,返回13
30 * 21.获取日期时间当月的总天数,如2017-02-13,返回28
31 * 22.获取日期时间当年的总天数,如2017-02-13,返回2017年的总天数
32 * 23.根据时间获取当月最大的日期
33 * 24.根据时间获取当月最小的日期,也就是返回当月的1号日期对象
34 */
35 public class DateUtils {
36
37 public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
38 public static final String MINUTE_PATTERN = "yyyy-MM-dd HH:mm";
39 public static final String HOUR_PATTERN = "yyyy-MM-dd HH:mm:ss";
40 public static final String DATE_PATTERN = "yyyy-MM-dd";
41 public static final String MONTH_PATTERN = "yyyy-MM";
42 public static final String YEAR_PATTERN = "yyyy";
43 public static final String MINUTE_ONLY_PATTERN = "mm";
44 public static final String HOUR_ONLY_PATTERN = "HH";
45
46 /**
47 * 日期相加减天数
48 *
49 * @param date 如果为Null,则为当前时间
50 * @param days 加减天数
51 * @param includeTime 是否包括时分秒,true表示包含
52 * @return
53 * @throws ParseException
54 */
55 public static Date dateAdd(Date date, int days, boolean includeTime) throws ParseException {
56 if (date == null) {
57 date = new Date();
58 }
59 if (!includeTime) {
60 SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_PATTERN);
61 date = sdf.parse(sdf.format(date));
62 }
63 Calendar cal = Calendar.getInstance();
64 cal.setTime(date);
65 cal.add(Calendar.DATE, days);
66 return cal.getTime();
67 }
68
69 /**
70 * 时间格式化成字符串
71 *
72 * @param date Date
73 * @param pattern StringUtils.DATE_TIME_PATTERN || StringUtils.DATE_PATTERN, 如果为空,则为yyyy-MM-dd
74 * @return
75 * @throws ParseException
76 */
77 public static String dateFormat(Date date, String pattern) throws ParseException {
78 if (StringUtils.isBlank(pattern)) {
79 pattern = DateUtils.DATE_PATTERN;
80 }
81 SimpleDateFormat sdf = new SimpleDateFormat(pattern);
82 return sdf.format(date);
83 }
84
85 /**
86 * 字符串解析成时间对象
87 *
88 * @param dateTimeString String
89 * @param pattern StringUtils.DATE_TIME_PATTERN || StringUtils.DATE_PATTERN,如果为空,则为yyyy-MM-dd
90 * @return
91 * @throws ParseException
92 */
93 public static Date dateParse(String dateTimeString, String pattern) throws ParseException {
94 if (StringUtils.isBlank(pattern)) {
95 pattern = DateUtils.DATE_PATTERN;
96 }
97 SimpleDateFormat sdf = new SimpleDateFormat(pattern);
98 return sdf.parse(dateTimeString);
99 }
100
101 /**
102 * 将日期时间格式成只有日期的字符串(可以直接使用dateFormat,Pattern为Null进行格式化)
103 *
104 * @param dateTime Date
105 * @return
106 * @throws ParseException
107 */
108 public static String dateTimeToDateString(Date dateTime) throws ParseException {
109 String dateTimeString = DateUtils.dateFormat(dateTime, DateUtils.DATE_TIME_PATTERN);
110 return dateTimeString.substring(0, 10);
111 }
112
113 /**
114 * 当时、分、秒为00:00:00时,将日期时间格式成只有日期的字符串,
115 * 当时、分、秒不为00:00:00时,直接返回
116 *
117 * @param dateTime Date
118 * @return
119 * @throws ParseException
120 */
121 public static String dateTimeToDateStringIfTimeEndZero(Date dateTime) throws ParseException {
122 String dateTimeString = DateUtils.dateFormat(dateTime, DateUtils.DATE_TIME_PATTERN);
123 if (dateTimeString.endsWith("00:00:00")) {
124 return dateTimeString.substring(0, 10);
125 } else {
126 return dateTimeString;
127 }
128 }
129
130 /**
131 * 将日期时间格式成日期对象,和dateParse互用
132 *
133 * @param dateTime Date
134 * @return Date
135 * @throws ParseException
136 */
137 public static Date dateTimeToDate(Date dateTime) throws ParseException {
138 Calendar cal = Calendar.getInstance();
139 cal.setTime(dateTime);
140 cal.set(Calendar.HOUR_OF_DAY, 0);
141 cal.set(Calendar.MINUTE, 0);
142 cal.set(Calendar.SECOND, 0);
143 cal.set(Calendar.MILLISECOND, 0);
144 return cal.getTime();
145 }
146
147 /**
148 * 时间加减小时
149 *
150 * @param startDate 要处理的时间,Null则为当前时间
151 * @param hours 加减的小时
152 * @return Date
153 */
154 public static Date dateAddHours(Date startDate, int hours) {
155 if (startDate == null) {
156 startDate = new Date();
157 }
158 Calendar c = Calendar.getInstance();
159 c.setTime(startDate);
160 c.set(Calendar.HOUR, c.get(Calendar.HOUR) + hours);
161 return c.getTime();
162 }
163
164 /**
165 * 时间加减分钟
166 *
167 * @param startDate 要处理的时间,Null则为当前时间
168 * @param minutes 加减的分钟
169 * @return
170 */
171 public static Date dateAddMinutes(Date startDate, int minutes) {
172 if (startDate == null) {
173 startDate = new Date();
174 }
175 Calendar c = Calendar.getInstance();
176 c.setTime(startDate);
177 c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes);
178 return c.getTime();
179 }
180
181 /**
182 * 时间加减秒数
183 *
184 * @param startDate 要处理的时间,Null则为当前时间
185 * @param minutes 加减的秒数
186 * @return
187 */
188 public static Date dateAddSeconds(Date startDate, int seconds) {
189 if (startDate == null) {
190 startDate = new Date();
191 }
192 Calendar c = Calendar.getInstance();
193 c.setTime(startDate);
194 c.set(Calendar.SECOND, c.get(Calendar.SECOND) + seconds);
195 return c.getTime();
196 }
197
198 /**
199 * 时间加减天数
200 *
201 * @param startDate 要处理的时间,Null则为当前时间
202 * @param days 加减的天数
203 * @return Date
204 */
205 public static Date dateAddDays(Date startDate, int days) {
206 if (startDate == null) {
207 startDate = new Date();
208 }
209 Calendar c = Calendar.getInstance();
210 c.setTime(startDate);
211 c.set(Calendar.DATE, c.get(Calendar.DATE) + days);
212 return c.getTime();
213 }
214
215 /**
216 * 时间加减月数
217 *
218 * @param startDate 要处理的时间,Null则为当前时间
219 * @param months 加减的月数
220 * @return Date
221 */
222 public static Date dateAddMonths(Date startDate, int months) {
223 if (startDate == null) {
224 startDate = new Date();
225 }
226 Calendar c = Calendar.getInstance();
227 c.setTime(startDate);
228 c.set(Calendar.MONTH, c.get(Calendar.MONTH) + months);
229 return c.getTime();
230 }
231
232 /**
233 * 时间加减年数
234 *
235 * @param startDate 要处理的时间,Null则为当前时间
236 * @param years 加减的年数
237 * @return Date
238 */
239 public static Date dateAddYears(Date startDate, int years) {
240 if (startDate == null) {
241 startDate = new Date();
242 }
243 Calendar c = Calendar.getInstance();
244 c.setTime(startDate);
245 c.set(Calendar.YEAR, c.get(Calendar.YEAR) + years);
246 return c.getTime();
247 }
248
249 /**
250 * 时间比较(如果myDate>compareDate返回1,<返回-1,相等返回0)
251 *
252 * @param myDate 时间
253 * @param compareDate 要比较的时间
254 * @return int
255 */
256 public static int dateCompare(Date myDate, Date compareDate) {
257 Calendar myCal = Calendar.getInstance();
258 Calendar compareCal = Calendar.getInstance();
259 myCal.setTime(myDate);
260 compareCal.setTime(compareDate);
261 return myCal.compareTo(compareCal);
262 }
263
264 /**
265 * 获取两个时间中最小的一个时间
266 *
267 * @param date
268 * @param compareDate
269 * @return
270 */
271 public static Date dateMin(Date date, Date compareDate) {
272 if (date == null) {
273 return compareDate;
274 }
275 if (compareDate == null) {
276 return date;
277 }
278 if (1 == dateCompare(date, compareDate)) {
279 return compareDate;
280 } else if (-1 == dateCompare(date, compareDate)) {
281 return date;
282 }
283 return date;
284 }
285
286 /**
287 * 获取两个时间中最大的一个时间
288 *
289 * @param date
290 * @param compareDate
291 * @return
292 */
293 public static Date dateMax(Date date, Date compareDate) {
294 if (date == null) {
295 return compareDate;
296 }
297 if (compareDate == null) {
298 return date;
299 }
300 if (1 == dateCompare(date, compareDate)) {
301 return date;
302 } else if (-1 == dateCompare(date, compareDate)) {
303 return compareDate;
304 }
305 return date;
306 }
307
308 /**
309 * 获取两个日期(不含时分秒)相差的天数,不包含今天
310 *
311 * @param startDate
312 * @param endDate
313 * @return
314 * @throws ParseException
315 */
316 public static int dateBetween(Date startDate, Date endDate) throws ParseException {
317 Date dateStart = dateParse(dateFormat(startDate, DATE_PATTERN), DATE_PATTERN);
318 Date dateEnd = dateParse(dateFormat(endDate, DATE_PATTERN), DATE_PATTERN);
319 return (int) ((dateEnd.getTime() - dateStart.getTime()) / 1000 / 60 / 60 / 24);
320 }
321
322 /**
323 * 获取两个日期(不含时分秒)相差的天数,包含今天
324 *
325 * @param startDate
326 * @param endDate
327 * @return
328 * @throws ParseException
329 */
330 public static int dateBetweenIncludeToday(Date startDate, Date endDate) throws ParseException {
331 return dateBetween(startDate, endDate) + 1;
332 }
333
334 /**
335 * 获取日期时间的年份,如2017-02-13,返回2017
336 *
337 * @param date
338 * @return
339 */
340 public static int getYear(Date date) {
341 Calendar cal = Calendar.getInstance();
342 cal.setTime(date);
343 return cal.get(Calendar.YEAR);
344 }
345
346 /**
347 * 获取日期时间的月份,如2017年2月13日,返回2
348 *
349 * @param date
350 * @return
351 */
352 public static int getMonth(Date date) {
353 Calendar cal = Calendar.getInstance();
354 cal.setTime(date);
355 return cal.get(Calendar.MONTH) + 1;
356 }
357
358 /**
359 * 获取日期时间的第几天(即返回日期的dd),如2017-02-13,返回13
360 *
361 * @param date
362 * @return
363 */
364 public static int getDate(Date date) {
365 Calendar cal = Calendar.getInstance();
366 cal.setTime(date);
367 return cal.get(Calendar.DATE);
368 }
369
370 /**
371 * 获取日期时间当月的总天数,如2017-02-13,返回28
372 *
373 * @param date
374 * @return
375 */
376 public static int getDaysOfMonth(Date date) {
377 Calendar cal = Calendar.getInstance();
378 cal.setTime(date);
379 return cal.getActualMaximum(Calendar.DATE);
380 }
381
382 /**
383 * 获取日期时间当年的总天数,如2017-02-13,返回2017年的总天数
384 *
385 * @param date
386 * @return
387 */
388 public static int getDaysOfYear(Date date) {
389 Calendar cal = Calendar.getInstance();
390 cal.setTime(date);
391 return cal.getActualMaximum(Calendar.DAY_OF_YEAR);
392 }
393
394 /**
395 * 根据时间获取当月最大的日期
396 * <li>2017-02-13,返回2017-02-28</li>
397 * <li>2016-02-13,返回2016-02-29</li>
398 * <li>2016-01-11,返回2016-01-31</li>
399 *
400 * @param date Date
401 * @return
402 * @throws Exception
403 */
404 public static Date maxDateOfMonth(Date date) throws Exception {
405 Calendar cal = Calendar.getInstance();
406 cal.setTime(date);
407 int value = cal.getActualMaximum(Calendar.DATE);
408 return dateParse(dateFormat(date, MONTH_PATTERN) + "-" + value, null);
409 }
410
411 /**
412 * 根据时间获取当月最小的日期,也就是返回当月的1号日期对象
413 *
414 * @param date Date
415 * @return
416 * @throws Exception
417 */
418 public static Date minDateOfMonth(Date date) throws Exception {
419 Calendar cal = Calendar.getInstance();
420 cal.setTime(date);
421 int value = cal.getActualMinimum(Calendar.DATE);
422 return dateParse(dateFormat(date, MONTH_PATTERN) + "-" + value, null);
423 }
424
425 }
来源:https://www.cnblogs.com/llawliet0001/p/9850474.html