boolean isEmpty(T[] array) {
35 | return (array == null || array.length == 0);
36 | }
37 |
38 | public static boolean isNotEmpty(T[] array) {
39 | return !isEmpty(array);
40 | }
41 |
42 | /**
43 | * Merge the given Properties instance into the given Map,
44 | * copying all properties (key-value pairs) over.
45 | * Uses {@code Properties.propertyNames()} to even catch
46 | * default properties linked into the original Properties instance.
47 | *
48 | * @param props the Properties instance to merge (may be {@code null})
49 | * @param map the target Map to merge the properties into
50 | */
51 | @SuppressWarnings("unchecked")
52 | public static void mergePropertiesIntoMap(Properties props, Map map) {
53 | if (map == null) {
54 | throw new IllegalArgumentException("Map must not be null");
55 | }
56 | if (props != null) {
57 | for (Enumeration> en = props.propertyNames(); en.hasMoreElements(); ) {
58 | String key = (String) en.nextElement();
59 | Object value = props.getProperty(key);
60 | if (value == null) {
61 | // Potentially a non-String value...
62 | value = props.get(key);
63 | }
64 | map.put((K) key, (V) value);
65 | }
66 | }
67 | }
68 |
69 | /**
70 | * Check whether the given Collection contains the given element instance.
71 | * Enforces the given instance to be present, rather than returning
72 | * {@code true} for an equal element as well.
73 | *
74 | * @param collection the Collection to check
75 | * @param element the element to look for
76 | * @return {@code true} if found, {@code false} else
77 | */
78 | public static boolean containsInstance(Collection> collection, Object element) {
79 | if (collection != null) {
80 | for (Object candidate : collection) {
81 | if (candidate == element) {
82 | return true;
83 | }
84 | }
85 | }
86 | return false;
87 | }
88 |
89 | /**
90 | * Return the first element in '{@code candidates}' that is contained in
91 | * '{@code source}'. If no element in '{@code candidates}' is present in
92 | * '{@code source}' returns {@code null}. Iteration order is
93 | * {@link Collection} implementation specific.
94 | *
95 | * @param source the source Collection
96 | * @param candidates the candidates to likeSearch for
97 | * @return the first present object, or {@code null} if not found
98 | */
99 | @SuppressWarnings("unchecked")
100 | public static E findFirstMatch(Collection> source, Collection candidates) {
101 | if (isEmpty(source) || isEmpty(candidates)) {
102 | return null;
103 | }
104 | for (Object candidate : candidates) {
105 | if (source.contains(candidate)) {
106 | return (E) candidate;
107 | }
108 | }
109 | return null;
110 | }
111 |
112 | /**
113 | * Find a single value of the given type in the given Collection.
114 | *
115 | * @param collection the Collection to likeSearch
116 | * @param type the type to look for
117 | * @return a value of the given type found if there is a clear match,
118 | * or {@code null} if none or more than one such value found
119 | */
120 | @SuppressWarnings("unchecked")
121 | public static T findValueOfType(Collection> collection, Class type) {
122 | if (isEmpty(collection)) {
123 | return null;
124 | }
125 | T value = null;
126 | for (Object element : collection) {
127 | if (type == null || type.isInstance(element)) {
128 | if (value != null) {
129 | // More than one value found... no clear single value.
130 | return null;
131 | }
132 | value = (T) element;
133 | }
134 | }
135 | return value;
136 | }
137 |
138 | /**
139 | * Find a single value of one of the given types in the given Collection:
140 | * searching the Collection for a value of the first type, then
141 | * searching for a value of the second type, etc.
142 | *
143 | * @param collection the collection to likeSearch
144 | * @param types the types to look for, in prioritized order
145 | * @return a value of one of the given types found if there is a clear match,
146 | * or {@code null} if none or more than one such value found
147 | */
148 | public static Object findValueOfType(Collection> collection, Class>[] types) {
149 | if (isEmpty(collection) || isEmpty(types)) {
150 | return null;
151 | }
152 | for (Class> type : types) {
153 | Object value = findValueOfType(collection, type);
154 | if (value != null) {
155 | return value;
156 | }
157 | }
158 | return null;
159 | }
160 |
161 | /**
162 | * Determine whether the given Collection only contains a single unique object.
163 | *
164 | * @param collection the Collection to check
165 | * @return {@code true} if the collection contains a single reference or
166 | * multiple references to the same instance, {@code false} else
167 | */
168 | public static boolean hasUniqueObject(Collection> collection) {
169 | if (isEmpty(collection)) {
170 | return false;
171 | }
172 | boolean hasCandidate = false;
173 | Object candidate = null;
174 | for (Object elem : collection) {
175 | if (!hasCandidate) {
176 | hasCandidate = true;
177 | candidate = elem;
178 | } else if (candidate != elem) {
179 | return false;
180 | }
181 | }
182 | return true;
183 | }
184 |
185 | /**
186 | * Find the common element type of the given Collection, if any.
187 | *
188 | * @param collection the Collection to check
189 | * @return the common element type, or {@code null} if no clear
190 | * common type has been found (or the collection was empty)
191 | */
192 | public static Class> findCommonElementType(Collection> collection) {
193 | if (isEmpty(collection)) {
194 | return null;
195 | }
196 | Class> candidate = null;
197 | for (Object val : collection) {
198 | if (val != null) {
199 | if (candidate == null) {
200 | candidate = val.getClass();
201 | } else if (candidate != val.getClass()) {
202 | return null;
203 | }
204 | }
205 | }
206 | return candidate;
207 | }
208 |
209 | /**
210 | * Marshal the elements from the given enumeration into an array of the given type.
211 | * Enumeration elements must be assignable to the type of the given array. The array
212 | * returned will be a different instance than the array given.
213 | */
214 | public static A[] toArray(Enumeration enumeration, A[] array) {
215 | ArrayList elements = new ArrayList<>();
216 | while (enumeration.hasMoreElements()) {
217 | elements.add(enumeration.nextElement());
218 | }
219 | return elements.toArray(array);
220 | }
221 |
222 | public static Map toMap(Collection elements, Function keyBuilder) {
223 | return toMap(elements, keyBuilder, Function.identity());
224 | }
225 |
226 | public static Map toMap(Collection elements, Function keyBuilder, Function valueFunc) {
227 | if (isEmpty(elements) || keyBuilder == null || valueFunc == null) {
228 | return Collections.emptyMap();
229 | }
230 | Map result = new HashMap<>(elements.size() * 3 / 2);
231 | for (E element : elements) {
232 | result.put(keyBuilder.apply(element), valueFunc.apply(element));
233 | }
234 | return result;
235 | }
236 |
237 | public static Map> toMapList(Collection elements, Function keyFunc) {
238 | return toMapList(elements, keyFunc, Function.identity());
239 | }
240 |
241 | public static Map> toMapList(Collection elements, Function keyFunc, Function valueFunc) {
242 | if (isEmpty(elements) || keyFunc == null || valueFunc == null) {
243 | return Collections.emptyMap();
244 | }
245 | Map> result = new HashMap<>(elements.size() * 3 / 2);
246 | for (E element : elements) {
247 | K key = keyFunc.apply(element);
248 | List values = result.computeIfAbsent(key, k -> new ArrayList<>());
249 | values.add(valueFunc.apply(element));
250 | }
251 | return result;
252 | }
253 |
254 | public static void forEach(Collection collection, Consumer super E> action) {
255 | if (isEmpty(collection) || action == null) {
256 | return;
257 | }
258 | collection.forEach(action);
259 | }
260 |
261 | public static List filter(Collection source, Predicate predicate) {
262 | if (isEmpty(source) || predicate == null) {
263 | return Collections.emptyList();
264 | }
265 | List result = new ArrayList<>();
266 | for (E element : source) {
267 | if (predicate.test(element)) {
268 | result.add(element);
269 | }
270 | }
271 | return result;
272 | }
273 |
274 |
275 | public static List selectList(Collection source, Function func) {
276 | return selectList(source, func, true);
277 | }
278 |
279 | public static List selectNotNullList(Collection source, Function func) {
280 | return selectList(source, func, false);
281 | }
282 |
283 | public static List selectList(Collection source, Function func, boolean isAllowNullValue) {
284 | if (isEmpty(source)) {
285 | return Collections.emptyList();
286 | }
287 | List resultList = new ArrayList<>(source.size());
288 | for (F f : source) {
289 | T t = func.apply(f);
290 | if (t != null || isAllowNullValue) {
291 | resultList.add(t);
292 | }
293 | }
294 | return resultList;
295 | }
296 |
297 | public static List selectListWithIndex(Collection source, BiFunction func) {
298 | if (isEmpty(source)) {
299 | return Collections.emptyList();
300 | }
301 | List resultList = new ArrayList<>(source.size());
302 | AtomicInteger index = new AtomicInteger();
303 | for (F f : source) {
304 | resultList.add(func.apply(f, index.getAndIncrement()));
305 | }
306 | return resultList;
307 | }
308 |
309 | public static R reduce(Collection elements, R initValue, BiFunction reduceFunc) {
310 | if (isEmpty(elements)) {
311 | return initValue;
312 | }
313 | R result = initValue;
314 | for (E element : elements) {
315 | if (null != element) {
316 | result = reduceFunc.apply(result, element);
317 | }
318 | }
319 | return result;
320 | }
321 |
322 | public static Map groupBy(Collection elements, Function keyFunc, BiFunction, R> resultBuilder) {
323 | if (isEmpty(elements) || keyFunc == null || resultBuilder == null) {
324 | return Collections.emptyMap();
325 | }
326 | Map> map = toMapList(elements, keyFunc, Function.identity());
327 |
328 | Map resultMap = new HashMap<>((int) (elements.size() * 1.75) + 1);
329 | for (Map.Entry> entry : map.entrySet()) {
330 | resultMap.put(entry.getKey(), resultBuilder.apply(entry.getKey(), entry.getValue()));
331 | }
332 | return resultMap;
333 | }
334 |
335 | public static List map(Collection source, Function func) {
336 | if (isEmpty(source)) {
337 | return Collections.emptyList();
338 | }
339 | List resultList = new ArrayList<>(source.size());
340 | for (F f : source) {
341 | T t = func.apply(f);
342 | resultList.add(t);
343 | }
344 | return resultList;
345 | }
346 | }
347 |
--------------------------------------------------------------------------------
/src/main/java/com/xjbg/java/sdk/util/DateUtil.java:
--------------------------------------------------------------------------------
1 | package com.xjbg.java.sdk.util;
2 |
3 | import com.xjbg.java.sdk.enums.DatePatternEnum;
4 | import org.joda.time.*;
5 | import org.joda.time.format.DateTimeFormat;
6 | import org.joda.time.format.DateTimeFormatter;
7 |
8 | import java.text.Format;
9 | import java.text.SimpleDateFormat;
10 | import java.util.*;
11 |
12 | /**
13 | * @author kesc
14 | * @since 2019/6/24
15 | */
16 | public final class DateUtil {
17 | private static final int[] CHINESE_WEEK = new int[]{0, 7, 1, 2, 3, 4, 5, 6};
18 |
19 |
20 | public static long getCurrentTimestamp() {
21 | return System.currentTimeMillis();
22 | }
23 |
24 | public static Date parseDateTime(String dateTime) {
25 | return parse(dateTime, DatePatternEnum.YYYYMMDDHHMMSS_BYSEP);
26 | }
27 |
28 | public static Date parseDate(String date) {
29 | return parse(date, DatePatternEnum.YYYYMMDD_BYSEP);
30 | }
31 |
32 | public static Date parseDateTime(String dateTime, TimeZone timeZone) {
33 | return parse(dateTime, DatePatternEnum.YYYYMMDDHHMMSS_BYSEP.getFormat(), timeZone);
34 | }
35 |
36 | public static Date parseDate(String date, TimeZone timeZone) {
37 | return parse(date, DatePatternEnum.YYYYMMDD_BYSEP.getFormat(), timeZone);
38 | }
39 |
40 | public static Date parse(String date, DatePatternEnum pattern) {
41 | return parse(date, pattern.getFormat());
42 | }
43 |
44 | public static Date parse(String date, String pattern) {
45 | return parse(date, pattern, TimeZone.getDefault());
46 | }
47 |
48 | public static Date parse(String date, DatePatternEnum pattern, TimeZone timeZone) {
49 | return parse(date, pattern.getFormat(), timeZone);
50 | }
51 |
52 | public static Date parse(String date, String pattern, TimeZone timeZone) {
53 | DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(pattern);
54 | DateTimeZone dateTimeZone = timeZone == null ? DateTimeZone.getDefault() : DateTimeZone.forTimeZone(timeZone);
55 | return dateFormatter.withZone(dateTimeZone).parseDateTime(date).toDate();
56 | }
57 |
58 | public static String format(Date date, String pattern) {
59 | return format(date, pattern, TimeZone.getDefault());
60 | }
61 |
62 | public static String format(Date date, DatePatternEnum datePatternEnum) {
63 | return format(date, datePatternEnum.getFormat());
64 | }
65 |
66 | public static String formatDateTime(Date date) {
67 | return format(date, DatePatternEnum.YYYYMMDDHHMMSS_BYSEP);
68 | }
69 |
70 | public static String formatDate(Date date) {
71 | return format(date, DatePatternEnum.YYYYMMDD_BYSEP);
72 | }
73 |
74 | public static String formatDateTime(Date date, TimeZone timeZone) {
75 | return format(date, DatePatternEnum.YYYYMMDDHHMMSS_BYSEP.getFormat(), timeZone);
76 | }
77 |
78 | public static String formatDate(Date date, TimeZone timeZone) {
79 | return format(date, DatePatternEnum.YYYYMMDD_BYSEP.getFormat(), timeZone);
80 | }
81 |
82 | public static String format(Date date, DatePatternEnum pattern, TimeZone timeZone) {
83 | return format(date, pattern.getFormat(), timeZone);
84 | }
85 |
86 | public static String format(Date date, String pattern, TimeZone timeZone) {
87 | DateTimeZone dateTimeZone = timeZone == null ? DateTimeZone.getDefault() : DateTimeZone.forTimeZone(timeZone);
88 | DateTime dateTime = new DateTime(date, dateTimeZone);
89 | return dateTime.toString(pattern);
90 | }
91 |
92 | public static Date getMinDate(Date date) {
93 | if (date == null) {
94 | return null;
95 | } else {
96 | DateTime dateTime = new DateTime(date);
97 | return dateTime.withTimeAtStartOfDay().toDate();
98 | }
99 | }
100 |
101 | public static Date getMaxDate(Date date) {
102 | if (date == null) {
103 | return null;
104 | } else {
105 | DateTime dateTime = new DateTime(date);
106 | return dateTime.millisOfDay().withMaximumValue().toDate();
107 | }
108 | }
109 |
110 | public static int betweenOfHours(Date firstDate, Date nextDate) {
111 | if (firstDate != null && nextDate != null) {
112 | LocalDateTime fist = new LocalDateTime(firstDate);
113 | LocalDateTime next = new LocalDateTime(nextDate);
114 | return Hours.hoursBetween(next, fist).getHours();
115 | } else {
116 | return 0;
117 | }
118 | }
119 |
120 | public static boolean inBetween(Date date, Date start, Date end) {
121 | if (date != null && start != null && end != null) {
122 | long st = start.getTime();
123 | long ed = end.getTime();
124 | long cur = date.getTime();
125 | return cur < ed && cur > st;
126 | } else {
127 | return false;
128 | }
129 | }
130 |
131 | public static boolean before(Date start, Date end) {
132 | DateTime startTime = new DateTime(start);
133 | DateTime endTime = new DateTime(end);
134 | return startTime.isBefore(endTime);
135 | }
136 |
137 | public static boolean after(Date start, Date end) {
138 | return !before(start, end);
139 | }
140 |
141 | public static String getMonthWithMaxDate(Date date) {
142 | DateTime dateTime = new DateTime(date);
143 | return dateTime.dayOfMonth().withMaximumValue().toString(DatePatternEnum.YYYYMMDD_BYSEP.getFormat());
144 | }
145 |
146 | public static String getYearWithMaxDate(Date date) {
147 | DateTime dateTime = new DateTime(date);
148 | return dateTime.monthOfYear().withMaximumValue().dayOfMonth().withMaximumValue().toString(DatePatternEnum.YYYYMMDD_BYSEP.getFormat());
149 | }
150 |
151 | public static String getMonthWithMinDate(Date date) {
152 | DateTime dateTime = new DateTime(date);
153 | return dateTime.dayOfMonth().withMinimumValue().toString(DatePatternEnum.YYYYMMDD_BYSEP.getFormat());
154 | }
155 |
156 | public static String getYearWithMinDate(Date date) {
157 | DateTime dateTime = new DateTime(date);
158 | return dateTime.monthOfYear().withMinimumValue().toString(DatePatternEnum.YYYYMMDD_BYSEP.getFormat());
159 | }
160 |
161 |
162 | public static int betweenOfDays(Date firstDate, Date nextDate) {
163 | if (firstDate != null && nextDate != null) {
164 | LocalDateTime fist = new LocalDateTime(firstDate);
165 | LocalDateTime next = new LocalDateTime(nextDate);
166 | return Days.daysBetween(next, fist).getDays() + 1;
167 | } else {
168 | return 0;
169 | }
170 | }
171 |
172 | public static Date addMilliSecond(Date date, int amount) {
173 | return (new DateTime(date)).plusMillis(amount).toDate();
174 | }
175 |
176 | public static Date addSecond(Date date, int amount) {
177 | return (new DateTime(date)).plusSeconds(amount).toDate();
178 | }
179 |
180 | public static Date addMinute(Date date, int amount) {
181 | return (new DateTime(date)).plusMinutes(amount).toDate();
182 | }
183 |
184 | public static Date addHour(Date date, int amount) {
185 | return (new DateTime(date)).plusHours(amount).toDate();
186 | }
187 |
188 | public static Date addDay(Date date, int amount) {
189 | return (new DateTime(date)).plusDays(amount).toDate();
190 | }
191 |
192 | public static Date addWeek(Date date, int amount) {
193 | return (new DateTime(date)).plusWeeks(amount).toDate();
194 | }
195 |
196 |
197 | public static Date addMonth(Date date, int amount) {
198 | return (new DateTime(date)).plusMonths(amount).toDate();
199 | }
200 |
201 | public static Date addYear(Date date, int amount) {
202 | return (new DateTime(date)).plusYears(amount).toDate();
203 | }
204 |
205 | public static Date minusMilliSecond(Date date, int amount) {
206 | return (new DateTime(date)).minusMillis(amount).toDate();
207 | }
208 |
209 | public static Date minusSecond(Date date, int amount) {
210 | return (new DateTime(date)).minusSeconds(amount).toDate();
211 | }
212 |
213 | public static Date minusMinute(Date date, int amount) {
214 | return (new DateTime(date)).minusMinutes(amount).toDate();
215 | }
216 |
217 | public static Date minusHour(Date date, int amount) {
218 | return (new DateTime(date)).minusHours(amount).toDate();
219 | }
220 |
221 | public static Date minusDay(Date date, int amount) {
222 | return (new DateTime(date)).minusDays(amount).toDate();
223 | }
224 |
225 | public static Date minusWeek(Date date, int amount) {
226 | return (new DateTime(date)).minusWeeks(amount).toDate();
227 | }
228 |
229 |
230 | public static Date minusMonth(Date date, int amount) {
231 | return (new DateTime(date)).minusMonths(amount).toDate();
232 | }
233 |
234 | public static Date minusYear(Date date, int amount) {
235 | return (new DateTime(date)).minusYears(amount).toDate();
236 | }
237 |
238 |
239 | public static Date now() {
240 | return new Date(getCurrentTimestamp());
241 | }
242 |
243 | public static long getDistanceMills(Date one, Date two) {
244 | long time1 = one.getTime();
245 | long time2 = two.getTime();
246 | long diff;
247 | if (time1 < time2) {
248 | diff = time2 - time1;
249 | } else {
250 | diff = time1 - time2;
251 | }
252 |
253 | return diff;
254 | }
255 |
256 | public static Date changeDate(Date date, DatePatternEnum from, DatePatternEnum to) {
257 | if (null == date) {
258 | return null;
259 | } else {
260 | DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(to.getFormat());
261 | return dateFormatter.parseDateTime((new DateTime(date)).toString(from.getFormat())).toDate();
262 | }
263 | }
264 |
265 | public static int getMinutesToEndDay(Date date) {
266 | DateTime startDate = new DateTime(date);
267 | DateTime endDate = (new DateTime(startDate.getYear(), startDate.getMonthOfYear(), startDate.getDayOfMonth(), 23, 59, 59, 59)).plusSeconds(1);
268 | Minutes minutes = Minutes.minutesBetween(startDate, endDate);
269 | return minutes.getMinutes();
270 | }
271 |
272 | /***
273 | * 获取时间在月份的天数
274 | * @param date
275 | * @return
276 | */
277 | public static int getDayOfMonth(Date date) {
278 | DateTime dateTime = new DateTime(date);
279 | return dateTime.getDayOfMonth();
280 | }
281 |
282 | /***
283 | * 月份的天数
284 | * @param date
285 | * @return
286 | */
287 | public static int daysOfMonth(Date date) {
288 | DateTime dateTime = new DateTime(date);
289 | return dateTime.dayOfMonth().getMaximumValue();
290 | }
291 |
292 | /**
293 | * 去年的最后一天
294 | *
295 | * @param date
296 | * @return
297 | */
298 | public static Date lastDayOfLastYear(Date date) {
299 | DateTime dateTime = new DateTime(date);
300 | DateTime lastYearDateTime = dateTime.minusYears(1);
301 | return lastYearDateTime.monthOfYear().withMaximumValue().dayOfMonth().withMaximumValue().toDate();
302 | }
303 |
304 | /***
305 | * 年的第一个天
306 | * @param date
307 | * @return
308 | */
309 | public static Date firstDayOfYear(Date date) {
310 | DateTime dateTime = new DateTime(date);
311 | return dateTime.monthOfYear().withMinimumValue().dayOfMonth().withMinimumValue().toDate();
312 | }
313 |
314 | /***
315 | * 年的最后一个天
316 | * @param date
317 | * @return
318 | */
319 | public static Date lastDayOfYear(Date date) {
320 | DateTime dateTime = new DateTime(date);
321 | return dateTime.monthOfYear().withMaximumValue().dayOfMonth().withMaximumValue().toDate();
322 | }
323 |
324 | /**
325 | * 获取从传入日期间隔中包含一年中的周数
326 | */
327 | public static List getBetweenWeeks(Date start, Date end) {
328 | if (end.before(start)) {
329 | return Collections.emptyList();
330 | }
331 | List list = new ArrayList<>();
332 | Format f = new SimpleDateFormat(DatePatternEnum.YYYYMMDD_BYSEP.getFormat());
333 | while (f.format(start).compareTo(f.format(end)) <= 0) {
334 | list.add(getWeekOfYear(start));
335 | start = DateUtil.addDay(start, 7);
336 | }
337 | return list;
338 | }
339 |
340 | public static String getWeekOfYear(Date date) {
341 | if (date == null) {
342 | return null;
343 | }
344 | DateTime dateTime = new DateTime(date);
345 | return dateTime.getYear() + StringUtil.MINUS + dateTime.getWeekOfWeekyear();
346 | }
347 |
348 | /**
349 | * 获取从传入日期间隔中包含一年中的月份数
350 | */
351 | public static List getBetweenMonth(Date start, Date end) {
352 | if (end.before(start)) {
353 | return Collections.emptyList();
354 | }
355 | List list = new ArrayList<>();
356 | Format f = new SimpleDateFormat(DatePatternEnum.YYYYMM_BYSEP.getFormat());
357 | while (f.format(start).compareTo(f.format(end)) <= 0) {
358 | list.add(f.format(start));
359 | start = DateUtil.addMonth(start, 1);
360 | }
361 | return list;
362 | }
363 |
364 | public static List getBetweenDates(Date start, Date end) {
365 | if (end.before(start)) {
366 | return Collections.emptyList();
367 | }
368 | List result = new ArrayList<>();
369 | Format f = new SimpleDateFormat(DatePatternEnum.YYYYMMDD_BYSEP.getFormat());
370 | while (f.format(start).compareTo(f.format(end)) <= 0) {
371 | result.add(f.format(start));
372 | start = DateUtil.addDay(start, 1);
373 | }
374 | return result;
375 | }
376 |
377 | public static List