潘志宝
2025-02-25 b3674cefbeb8ffaf49f96d0c8fe7a6694ed86eec
提交 | 用户 | 时间
1c9291 1 /**
L 2  * Copyright (c) 2018 人人开源 All rights reserved.
3  * <p>
4  * https://www.renren.io
5  * <p>
6  * 版权所有,侵权必究!
7  */
8
9 package com.iailab.module.model.common.utils;
10
11 import org.apache.commons.lang3.StringUtils;
12 import org.joda.time.DateTime;
13 import org.joda.time.LocalDate;
14 import org.joda.time.format.DateTimeFormat;
15 import org.joda.time.format.DateTimeFormatter;
16
17 import java.text.ParseException;
18 import java.text.SimpleDateFormat;
19 import java.util.*;
20
21 /**
22  * 日期处理
23  *
24  * @author Mark sunlightcs@gmail.com
25  */
26 public class DateUtils {
27     /** 时间格式(yyyy-MM-dd) */
28     public final static String DATE_PATTERN = "yyyy-MM-dd";
29     /** 时间格式(yyyy-MM-dd HH:mm:ss) */
30     public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
31
32     public final static String DATE_TIME_PATTERN_MIN = "yyyy-MM-dd HH:mm";
33
34     public final static String TIME2_PATTERN = "mmss";
35
36     public final static String DATE_PATTERN_MON = "yyyy-MM";
37
38     public final static String DATE_PATTERN_YEAR = "yyyy";
39
1ab119 40     public final static String DATE_TIME_STRING = "yyyyMMddHHmmssSSS";
D 41
1c9291 42     /**
L 43      * 日期格式化 日期格式为:yyyy-MM-dd
44      * @param date  日期
45      * @return 返回yyyy-MM-dd格式日期
46      */
47     public static String format(Date date) {
48         return format(date, DATE_PATTERN);
49     }
50
51     /**
52      * 日期格式化 日期格式为:yyyy-MM-dd
53      * @param date  日期
54      * @param pattern  格式,如:DateUtils.DATE_TIME_PATTERN
55      * @return 返回yyyy-MM-dd格式日期
56      */
57     public static String format(Date date, String pattern) {
58         if (date != null) {
59             SimpleDateFormat df = new SimpleDateFormat(pattern);
60             return df.format(date);
61         }
62         return null;
63     }
64
65     /**
66      * 日期解析
67      * @param date  日期
68      * @param pattern  格式,如:DateUtils.DATE_TIME_PATTERN
69      * @return 返回Date
70      */
71     public static Date parse(String date, String pattern) {
72         try {
73             return new SimpleDateFormat(pattern).parse(date);
74         } catch (ParseException e) {
75             e.printStackTrace();
76         }
77         return null;
78     }
79
80     /**
81      * 字符串转换成日期
82      * @param strDate 日期字符串
83      * @param pattern 日期的格式,如:DateUtils.DATE_TIME_PATTERN
84      */
85     public static Date stringToDate(String strDate, String pattern) {
86         if (StringUtils.isBlank(strDate)) {
87             return null;
88         }
89
90         DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
91         return fmt.parseLocalDateTime(strDate).toDate();
92     }
93
94     /**
95      * 根据周数,获取开始日期、结束日期
96      * @param week  周期  0本周,-1上周,-2上上周,1下周,2下下周
97      * @return 返回date[0]开始日期、date[1]结束日期
98      */
99     public static Date[] getWeekStartAndEnd(int week) {
100         DateTime dateTime = new DateTime();
101         LocalDate date = new LocalDate(dateTime.plusWeeks(week));
102
103         date = date.dayOfWeek().withMinimumValue();
104         Date beginDate = date.toDate();
105         Date endDate = date.plusDays(6).toDate();
106         return new Date[]{beginDate, endDate};
107     }
108
109     /**
110      * 对日期的【秒】进行加/减
111      *
112      * @param date 日期
113      * @param seconds 秒数,负数为减
114      * @return 加/减几秒后的日期
115      */
116     public static Date addDateSeconds(Date date, int seconds) {
117         DateTime dateTime = new DateTime(date);
118         return dateTime.plusSeconds(seconds).toDate();
119     }
120
121     /**
122      * 对日期的【分钟】进行加/减
123      *
124      * @param date 日期
125      * @param minutes 分钟数,负数为减
126      * @return 加/减几分钟后的日期
127      */
128     public static Date addDateMinutes(Date date, int minutes) {
129         DateTime dateTime = new DateTime(date);
130         return dateTime.plusMinutes(minutes).toDate();
131     }
132
133     /**
134      * 对日期的【小时】进行加/减
135      *
136      * @param date 日期
137      * @param hours 小时数,负数为减
138      * @return 加/减几小时后的日期
139      */
140     public static Date addDateHours(Date date, int hours) {
141         DateTime dateTime = new DateTime(date);
142         return dateTime.plusHours(hours).toDate();
143     }
144
145     /**
146      * 对日期的【天】进行加/减
147      *
148      * @param date 日期
149      * @param days 天数,负数为减
150      * @return 加/减几天后的日期
151      */
152     public static Date addDateDays(Date date, int days) {
153         DateTime dateTime = new DateTime(date);
154         return dateTime.plusDays(days).toDate();
155     }
156
157     /**
158      * 对日期的【周】进行加/减
159      *
160      * @param date 日期
161      * @param weeks 周数,负数为减
162      * @return 加/减几周后的日期
163      */
164     public static Date addDateWeeks(Date date, int weeks) {
165         DateTime dateTime = new DateTime(date);
166         return dateTime.plusWeeks(weeks).toDate();
167     }
168
169     /**
170      * 对日期的【月】进行加/减
171      *
172      * @param date 日期
173      * @param months 月数,负数为减
174      * @return 加/减几月后的日期
175      */
176     public static Date addDateMonths(Date date, int months) {
177         DateTime dateTime = new DateTime(date);
178         return dateTime.plusMonths(months).toDate();
179     }
180
181     /**
182      * 对日期的【年】进行加/减
183      *
184      * @param date 日期
185      * @param years 年数,负数为减
186      * @return 加/减几年后的日期
187      */
188     public static Date addDateYears(Date date, int years) {
189         DateTime dateTime = new DateTime(date);
190         return dateTime.plusYears(years).toDate();
191     }
192
193     public static List<String> getDays(Date startDate, Date endDate) {
194         List<String> days = new ArrayList<String>();
195         Calendar calendar = Calendar.getInstance();
196         calendar.setTime(startDate);
197
198         while (calendar.getTime().compareTo(endDate) < 0) {
199             days.add(DateUtils.format(calendar.getTime(), "yyyy-MM-dd"));
200             calendar.add(6, 1);
201         }
202         return days;
203     }
204
205     public static Date getTime(Date date) {
206         Calendar calendar = Calendar.getInstance();
207         calendar.setTime(date);
208         return calendar.getTime();
209     }
210
211     public static List<String> getYearTime(Date startDate, Date endDate) {
212         List<String> days = new ArrayList<String>();
213         Calendar calendar = Calendar.getInstance();
214         calendar.setTime(startDate);
215
216         while (calendar.getTime().compareTo(endDate) <= 0) {
217             days.add(DateUtils.format(calendar.getTime(), DATE_PATTERN_YEAR));
218             calendar.add(Calendar.YEAR, 1);
219         }
220         return days;
221     }
222
223     public static List<String> getMonTime(Date startDate, Date endDate) {
224         List<String> days = new ArrayList<String>();
225         Calendar calendar = Calendar.getInstance();
226         calendar.setTime(startDate);
227
228         while (calendar.getTime().compareTo(endDate) <= 0) {
229             days.add(DateUtils.format(calendar.getTime(), DATE_PATTERN_MON));
230             calendar.add(Calendar.MONTH, 1);
231         }
232         return days;
233     }
234
235     public static List<String> getDayTime(Date startDate, Date endDate) {
236         List<String> days = new ArrayList<String>();
237         Calendar calendar = Calendar.getInstance();
238         calendar.setTime(startDate);
239
240         while (calendar.getTime().compareTo(endDate) <= 0) {
241             days.add(DateUtils.format(calendar.getTime(), DATE_PATTERN));
242             calendar.add(Calendar.DAY_OF_YEAR, 1);
243         }
244         return days;
245     }
246
247     public static List<String> getDayTime(Date startDate, Date endDate, int seconds) {
248         List<String> days = new ArrayList<String>();
249         Calendar calendar = Calendar.getInstance();
250         calendar.setTime(startDate);
251         while (calendar.getTime().compareTo(endDate) <= 0) {
252             days.add(DateUtils.format(calendar.getTime(), DATE_TIME_PATTERN));
253             calendar.add(Calendar.SECOND, seconds);
254         }
255         return days;
256     }
257
258     public static List<String> getClassTime(Date startDate, Date endDate) {
259         List<String> days = new ArrayList<String>();
260         Calendar calendar = Calendar.getInstance();
261         calendar.setTime(startDate);
262         while (calendar.getTime().compareTo(endDate) <= 0) {
263             days.add(DateUtils.format(calendar.getTime(), DATE_PATTERN) + " 0点班");
264             days.add(DateUtils.format(calendar.getTime(), DATE_PATTERN) + " 8点班");
265             days.add(DateUtils.format(calendar.getTime(), DATE_PATTERN) + " 16点班");
266             calendar.add(Calendar.DAY_OF_YEAR, 1);
267         }
268         return days;
269     }
270
271
272     public static Map<String, Date> getIntervalDate(int days) {
273         Calendar calendar = Calendar.getInstance();
274         calendar.add(Calendar.DAY_OF_YEAR, days);
275         calendar.set(Calendar.MILLISECOND, 0);
276         calendar.set(Calendar.SECOND, 0);
277         Date tEndDate = calendar.getTime();
278         calendar.add(Calendar.HOUR_OF_DAY, -1);
279         Date tStartDate = calendar.getTime();
280         Map<String, Date> tMap = new HashMap<String, Date>(2);
281         tMap.put("startdate", tStartDate);
282         tMap.put("enddate", tEndDate);
283         return tMap;
284
285     }
286
287     public static boolean isNotBlank(Date date) {
288         if (date == null) {
289             return false;
290         }
291         return true;
292     }
293
294 }