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