DateUtil.java
7.8 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
package cn.com.glsx.entmicro.util;
import com.glsx.plat.common.utils.DateUtils;
import com.glsx.plat.common.utils.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import scala.util.parsing.combinator.testing.Str;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class DateUtil extends org.apache.commons.lang.time.DateUtils {
public static final String NORMAL_DATE_PATTERN;
public static final String reg = "(\\d{4}[/-]?\\d{1,2}[/-]?\\d{1,2})?";
public static final String reg1 = "(\\d{4}[/-]?\\d{1,2}[/-]?\\d{1,2} \\d{1,2}:\\d{1,2}[:]?\\d{1,2})?";
public static final String reg2 = "(\\d{14})?";
public static final String reg3 = "(\\d{4}[/-]?\\d{1,2}[/-]?\\d{1,2} \\d{1,2}:\\d{1,2}[:]?\\d{1,2})?";
public static final String reg4 = "(\\d{14})?";
static {
NORMAL_DATE_PATTERN = DateFormatUtils.ISO_DATE_FORMAT.getPattern();
}
//日期比较方法
public static int compareDate(Date first, Date second) {
return first.compareTo(second);
}
//获取近datNum天日期,从今日
public static List<String> getDates(int datNum, boolean fromNow) {
int index = 1;
int end = datNum + 1;
if (fromNow) {
index = 0;
end = datNum;
}
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
for (int i = index; i < end; i++) {
Date date = DateUtils.addDays(new Date(), -i);
String formatDate = sdf.format(date);
dateList.add(formatDate);
}
return dateList;
}
//获取近datNum天日期,从今日
public static List<String> getDates(Date dateEnd, int datNum) {
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
for (int i = 0; i < datNum; i++) {
Date date = DateUtils.addDays(dateEnd, -i);
String formatDate = sdf.format(date);
dateList.add(formatDate);
}
return dateList;
}
public static Date parse(String strDate) {
Date date = null;
try {
if (StringUtils.isNotEmpty(strDate)) {
if (strDate.matches(reg3)) {
date = parseDate(strDate, new String[]{NORMAL_DATE_PATTERN, "yyyy-MM-dd HH:mm:ss"});
} else {
date = parseDate(strDate, new String[]{NORMAL_DATE_PATTERN, "yyyy/MM/dd"});
}
}
} catch (ParseException var3) {
var3.printStackTrace();
}
return date;
}
public static Set<String> getMonths(int datNum) {
Set<String> dateList = new HashSet<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");
for (int i = 0; i < datNum; i++) {
Date date = DateUtils.addDays(new Date(), -i);
String formatDate = sdf.format(date);
dateList.add(formatDate);
}
return dateList;
}
public static Integer getMonthDays(String date) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");
try {
cal.setTime(sdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
/* cal.set(Calendar.YEAR, 2002 );
cal.set(Calendar.MONTH, 6 ); // 7月*/
int maxDate = cal.getActualMaximum(Calendar.DATE);
return maxDate;
}
public static List<String> getAllDates(String minDay) {
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
int i = 0;
while (true) {
Date date = DateUtils.addDays(new Date(), -i);
String formatDate = sdf.format(date);
System.out.println("最小日期" + minDay);
System.out.println("当前日期" + formatDate);
dateList.add(formatDate);
if (formatDate.equals(minDay)) {
break;
}
i++;
}
return dateList;
}
/**
* 合并日期
*
* @return
*/
public static Date mergeDate(Date date, String datetime) {
SimpleDateFormat ds = new SimpleDateFormat("yyyy/MM/dd HH:mm");
SimpleDateFormat dsp = new SimpleDateFormat("yyyy/MM/dd");
try {
String format = dsp.format(date.getTime());
Date endTime = ds.parse(format + " " + datetime);
return endTime;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static List<String> getDateTimeList() {
List<String> timelist = new ArrayList<>();
for (int i = 0; i < 25; i++) {
if (i < 10) {
timelist.add("0" + i + ":00");
} else {
timelist.add(i + ":00");
}
}
return timelist;
}
/**
* 获取时间段内的时间集合
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return
*/
public static List<String> getDateList(String startTime, String endTime) {
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = DateUtils.stringToDate(startTime, "yyyy-MM-dd");
int i = 0;
while (true) {
Date date = DateUtils.addDays(startDate, +i);
String formatDate = sdf.format(date);
dateList.add(formatDate);
if (formatDate.equals(endTime)) {
break;
}
i++;
}
return dateList;
}
/**
* 获取时间段内的时间集合
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return
*/
public static List<String> getDateList(Date startTime, Date endTime) {
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String endDate = sdf.format(endTime);
int i = 0;
while (true) {
Date date = DateUtils.addDays(startTime, +i);
String formatDate = sdf.format(date);
dateList.add(formatDate);
if (formatDate.equals(endDate)) {
break;
}
i++;
}
return dateList;
}
public static boolean isDate(String str) {
String reg = "(\\d{4}[/-]?\\d{1,2}[/-]?\\d{1,2})?";
String reg1 = "(\\d{4}[/-]?\\d{1,2}[/-]?\\d{1,2} \\d{1,2}:\\d{1,2}[:]?\\d{1,2})?";
String reg2 = "(\\d{14})?";
boolean matches = str.matches(reg);
if (matches) {
return true;
}
boolean matches1 = str.matches(reg1);
if (matches1) {
return true;
}
return str.matches(reg2);
}
/**
* 计算日期相差分钟数
* @param start
* @param end
* @return
*/
public static long diffMinute(Date start, Date end) {
if (start == null || end == null) {
return 0;
}
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(start);
c2.setTime(end);
if (c1.after(c2)) {
return 0;
} else {
long diff = c2.getTimeInMillis() - c1.getTimeInMillis();
return diff / (60 * 1000);
}
}
public static String timeStampSecToDateStr(String timestampString) {
try {
long parseLong = Long.parseLong(timestampString);
long timestamp = parseLong * 1000L;
return DateFormatUtils.format(timestamp, "yyyy-MM-dd HH:mm:ss");
} catch (Exception var6) {
var6.printStackTrace();
return "";
}
}
}