广丰卷烟厂数采质量分析系统
baoshiwei
16 小时以前 d143af7023cfd4a0ced6f0ecf04ae3b3a06fd1dc
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package org.dromara.common.core.utils;
 
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.dromara.common.core.enums.FormatsType;
import org.dromara.common.core.exception.ServiceException;
 
import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;
import java.util.concurrent.TimeUnit;
 
/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    private static final String[] PARSE_PATTERNS = {
        "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
        "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
        "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
 
    @Deprecated
    private DateUtils() {
    }
 
    /**
     * 获取当前日期和时间
     *
     * @return 当前日期和时间的 Date 对象表示
     */
    public static Date getNowDate() {
        return new Date();
    }
 
    /**
     * 获取当前日期的字符串表示,格式为YYYY-MM-DD
     *
     * @return 当前日期的字符串表示
     */
    public static String getDate() {
        return dateTimeNow(FormatsType.YYYY_MM_DD);
    }
 
    /**
     * 获取当前日期的字符串表示,格式为yyyyMMdd
     *
     * @return 当前日期的字符串表示
     */
    public static String getCurrentDate() {
        return DateFormatUtils.format(new Date(), FormatsType.YYYYMMDD.getTimeFormat());
    }
 
    /**
     * 获取当前日期的路径格式字符串,格式为"yyyy/MM/dd"
     *
     * @return 当前日期的路径格式字符串
     */
    public static String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, FormatsType.YYYY_MM_DD_SLASH.getTimeFormat());
    }
 
    /**
     * 获取当前时间的字符串表示,格式为YYYY-MM-DD HH:MM:SS
     *
     * @return 当前时间的字符串表示
     */
    public static String getTime() {
        return dateTimeNow(FormatsType.YYYY_MM_DD_HH_MM_SS);
    }
 
    /**
     * 获取当前时间的字符串表示,格式为 "HH:MM:SS"
     *
     * @return 当前时间的字符串表示,格式为 "HH:MM:SS"
     */
    public static String getTimeWithHourMinuteSecond() {
        return dateTimeNow(FormatsType.HH_MM_SS);
    }
 
    /**
     * 获取当前日期和时间的字符串表示,格式为YYYYMMDDHHMMSS
     *
     * @return 当前日期和时间的字符串表示
     */
    public static String dateTimeNow() {
        return dateTimeNow(FormatsType.YYYYMMDDHHMMSS);
    }
 
    /**
     * 获取当前日期和时间的指定格式的字符串表示
     *
     * @param format 日期时间格式,例如"YYYY-MM-DD HH:MM:SS"
     * @return 当前日期和时间的字符串表示
     */
    public static String dateTimeNow(final FormatsType format) {
        return parseDateToStr(format, new Date());
    }
 
    /**
     * 将指定日期格式化为 YYYY-MM-DD 格式的字符串
     *
     * @param date 要格式化的日期对象
     * @return 格式化后的日期字符串
     */
    public static String formatDate(final Date date) {
        return parseDateToStr(FormatsType.YYYY_MM_DD, date);
    }
 
    /**
     * 将指定日期格式化为 YYYY-MM-DD HH:MM:SS 格式的字符串
     *
     * @param date 要格式化的日期对象
     * @return 格式化后的日期时间字符串
     */
    public static String formatDateTime(final Date date) {
        return parseDateToStr(FormatsType.YYYY_MM_DD_HH_MM_SS, date);
    }
 
    /**
     * 将指定日期按照指定格式进行格式化
     *
     * @param format 要使用的日期时间格式,例如"YYYY-MM-DD HH:MM:SS"
     * @param date   要格式化的日期对象
     * @return 格式化后的日期时间字符串
     */
    public static String parseDateToStr(final FormatsType format, final Date date) {
        return new SimpleDateFormat(format.getTimeFormat()).format(date);
    }
 
    /**
     * 将指定格式的日期时间字符串转换为 Date 对象
     *
     * @param format 要解析的日期时间格式,例如"YYYY-MM-DD HH:MM:SS"
     * @param ts     要解析的日期时间字符串
     * @return 解析后的 Date 对象
     * @throws RuntimeException 如果解析过程中发生异常
     */
    public static Date parseDateTime(final FormatsType format, final String ts) {
        try {
            return new SimpleDateFormat(format.getTimeFormat()).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 将对象转换为日期对象
     *
     * @param str 要转换的对象,通常是字符串
     * @return 转换后的日期对象,如果转换失败或输入为null,则返回null
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), PARSE_PATTERNS);
        } catch (ParseException e) {
            return null;
        }
    }
 
    /**
     * 获取服务器启动时间
     *
     * @return 服务器启动时间的 Date 对象表示
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }
 
    /**
     * 计算两个时间之间的时间差,并以指定单位返回(绝对值)
     *
     * @param start 起始时间
     * @param end   结束时间
     * @param unit  所需返回的时间单位(DAYS、HOURS、MINUTES、SECONDS、MILLISECONDS、MICROSECONDS、NANOSECONDS)
     * @return 时间差的绝对值,以指定单位表示
     */
    public static long difference(Date start, Date end, TimeUnit unit) {
        // 计算时间差,单位为毫秒,取绝对值避免负数
        long diffInMillis = Math.abs(end.getTime() - start.getTime());
 
        // 根据目标单位转换时间差
        return switch (unit) {
            case DAYS -> diffInMillis / TimeUnit.DAYS.toMillis(1);
            case HOURS -> diffInMillis / TimeUnit.HOURS.toMillis(1);
            case MINUTES -> diffInMillis / TimeUnit.MINUTES.toMillis(1);
            case SECONDS -> diffInMillis / TimeUnit.SECONDS.toMillis(1);
            case MILLISECONDS -> diffInMillis;
            case MICROSECONDS -> TimeUnit.MILLISECONDS.toMicros(diffInMillis);
            case NANOSECONDS -> TimeUnit.MILLISECONDS.toNanos(diffInMillis);
        };
    }
 
    // 获取两个时间的差值
    public static long getTimeDifference( Date nowDate,Date endDate, TimeUnit unit) {
        // 计算时间差,单位为毫秒,取绝对值避免负数
        long diffInMillis = endDate.getTime() - nowDate.getTime();
 
        // 根据目标单位转换时间差
        return switch (unit) {
            case DAYS -> diffInMillis / TimeUnit.DAYS.toMillis(1);
            case HOURS -> diffInMillis / TimeUnit.HOURS.toMillis(1);
            case MINUTES -> diffInMillis / TimeUnit.MINUTES.toMillis(1);
            case SECONDS -> diffInMillis / TimeUnit.SECONDS.toMillis(1);
            case MILLISECONDS -> diffInMillis;
            case MICROSECONDS -> TimeUnit.MILLISECONDS.toMicros(diffInMillis);
            case NANOSECONDS -> TimeUnit.MILLISECONDS.toNanos(diffInMillis);
        };
    }
 
    /**
     * 计算两个日期之间的时间差,并以天、小时和分钟的格式返回
     *
     * @param endDate 结束日期
     * @param nowDate 当前日期
     * @return 表示时间差的字符串,格式为"天 小时 分钟"
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long diffInMillis = endDate.getTime() - nowDate.getTime();
        long day = TimeUnit.MILLISECONDS.toDays(diffInMillis);
        long hour = TimeUnit.MILLISECONDS.toHours(diffInMillis) % 24;
        long min = TimeUnit.MILLISECONDS.toMinutes(diffInMillis) % 60;
        return String.format("%d天 %d小时 %d分钟", day, hour, min);
    }
 
    /**
     * 计算两个时间点的差值(天、小时、分钟、秒),当值为0时不显示该单位
     *
     * @param endDate 结束时间
     * @param nowDate 当前时间
     * @return 时间差字符串,格式为 "x天 x小时 x分钟 x秒",若为 0 则不显示
     */
    public static String getTimeDifference(Date endDate, Date nowDate) {
        long diffInMillis = endDate.getTime() - nowDate.getTime();
        long day = TimeUnit.MILLISECONDS.toDays(diffInMillis);
        long hour = TimeUnit.MILLISECONDS.toHours(diffInMillis) % 24;
        long min = TimeUnit.MILLISECONDS.toMinutes(diffInMillis) % 60;
        long sec = TimeUnit.MILLISECONDS.toSeconds(diffInMillis) % 60;
        // 构建时间差字符串,条件是值不为0才显示
        StringBuilder result = new StringBuilder();
        if (day > 0) {
            result.append(String.format("%d天 ", day));
        }
        if (hour > 0) {
            result.append(String.format("%d小时 ", hour));
        }
        if (min > 0) {
            result.append(String.format("%d分钟 ", min));
        }
        if (sec > 0) {
            result.append(String.format("%d秒", sec));
        }
        return result.length() > 0 ? result.toString().trim() : "0秒";
    }
 
    /**
     * 将 LocalDateTime 对象转换为 Date 对象
     *
     * @param temporalAccessor 要转换的 LocalDateTime 对象
     * @return 转换后的 Date 对象
     */
    public static Date toDate(LocalDateTime temporalAccessor) {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }
 
    /**
     * 将 LocalDate 对象转换为 Date 对象
     *
     * @param temporalAccessor 要转换的 LocalDate 对象
     * @return 转换后的 Date 对象
     */
    public static Date toDate(LocalDate temporalAccessor) {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }
 
    /**
     * 校验日期范围
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @param maxValue  最大时间跨度的限制值
     * @param unit      时间跨度的单位,可选择 "DAYS"、"HOURS" 或 "MINUTES"
     */
    public static void validateDateRange(Date startDate, Date endDate, int maxValue, TimeUnit unit) {
        // 校验结束日期不能早于开始日期
        if (endDate.before(startDate)) {
            throw new ServiceException("结束日期不能早于开始日期");
        }
 
        // 计算时间跨度
        long diffInMillis = endDate.getTime() - startDate.getTime();
 
        // 根据单位转换时间跨度
        long diff = switch (unit) {
            case DAYS -> TimeUnit.MILLISECONDS.toDays(diffInMillis);
            case HOURS -> TimeUnit.MILLISECONDS.toHours(diffInMillis);
            case MINUTES -> TimeUnit.MILLISECONDS.toMinutes(diffInMillis);
            default -> throw new IllegalArgumentException("不支持的时间单位");
        };
 
        // 校验时间跨度不超过最大限制
        if (diff > maxValue) {
            throw new ServiceException("最大时间跨度为 {} {}", maxValue, unit.toString().toLowerCase());
        }
    }
 
    /**
     * 根据指定日期时间获取时间段(凌晨 / 上午 / 中午 / 下午 / 晚上)
     *
     * @param date 日期时间
     * @return 时间段描述
     */
    public static String getTodayHour(Date date) {
        int hour = DateUtil.hour(date, true);
        if (hour <= 6) {
            return "凌晨";
        } else if (hour < 12) {
            return "上午";
        } else if (hour == 12) {
            return "中午";
        } else if (hour <= 18) {
            return "下午";
        } else {
            return "晚上";
        }
    }
 
    /**
     * 将日期格式化为仿微信的友好时间
     * <p>
     * 规则说明:
     * 1. 未来时间:yyyy-MM-dd HH:mm
     * 2. 今天:
     * - 1 分钟内:刚刚
     * - 1 小时内:X 分钟前
     * - 超过 1 小时:凌晨/上午/中午/下午/晚上 HH:mm
     * 3. 昨天:昨天 HH:mm
     * 4. 本周:周X HH:mm
     * 5. 今年内:MM-dd HH:mm
     * 6. 非今年:yyyy-MM-dd HH:mm
     *
     * @param date 日期时间
     * @return 格式化后的时间描述
     */
    public static String formatFriendlyTime(Date date) {
        if (date == null) {
            return "";
        }
        Date now = DateUtil.date();
 
        // 未来时间或非今年
        if (date.after(now) || DateUtil.year(date) != DateUtil.year(now)) {
            return parseDateToStr(FormatsType.YYYY_MM_DD_HH_MM, date);
        }
 
        // 今天
        if (DateUtil.isSameDay(date, now)) {
            long minutes = DateUtil.between(date, now, DateUnit.MINUTE);
            if (minutes < 1) {
                return "刚刚";
            }
            if (minutes < 60) {
                return minutes + "分钟前";
            }
            return getTodayHour(date) + " " + DateUtil.format(date, "HH:mm");
        }
 
        // 昨天
        if (DateUtil.isSameDay(date, DateUtil.yesterday())) {
            return "昨天 " + DateUtil.format(date, "HH:mm");
        }
 
        // 本周
        if (DateUtil.isSameWeek(date, now, true)) {
            return DateUtil.dayOfWeekEnum(date).toChinese("周")
                + " " + DateUtil.format(date, "HH:mm");
        }
 
        // 今年内其它时间
        return DateUtil.format(date, "MM-dd HH:mm");
    }
 
}