付延余
2022-12-16 f0f8ee8c4a945adbc742d9bab69382b28ad311fb
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
package com.wgcloud.util;
 
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
 
import static com.cronutils.model.CronType.QUARTZ;
 
 
/**
 * @version v3.3
 * @ClassName:DateUtil.java
 * @author: http://www.wgstart.com
 * @date: 2021年1月16日
 * @Description: DateUtil.java
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
public class DateUtil {
    private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
 
    private static final String DATE_PATTERN = "yyyy-MM-dd";
    private static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    private static final String DATETIME_PATTERN_ZH = "yyyy年MM月dd日HH:mm:ss";
 
 
    /**
     * 获取当前时间
     *
     * @return 当前日期
     */
    public static Timestamp getNowTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATETIME_PATTERN);
        Timestamp nowTime = Timestamp.valueOf(dateFormat.format(new Date()));
        return nowTime;
    }
 
 
    /**
     * 获取当前系统时间.
     * 默认模板格式yyyy-MM-dd hh:mm:ss.
     *
     * @return 当前系统时间
     */
    public static String getCurrentDateTime() {
        return getCurrentDateTime(DATETIME_PATTERN);
    }
 
    /**
     * 获取当前系统同期。
     *
     * @return 当前系统日期
     */
    public static String getCurrentDate() {
        return getCurrentDateTime(DATE_PATTERN);
    }
 
    /**
     * 获取当前系统时间.
     *
     * @return 当前系统时间
     */
    public static String getCurrentDateTime(String pattern) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(cal.getTime());
    }
 
    public static Date getDate(String dateStr) throws ParseException {
        return getDate(dateStr, DATETIME_PATTERN);
    }
 
    public static Date getDate(String dateStr, String pattern) throws
            ParseException {
        Date date = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        date = dateFormat.parse(dateStr);
 
        return date;
    }
 
    public static String getDateString(Date date) {
        return getString(date, DATE_PATTERN);
    }
 
    public static String getDateTimeString(Date date) {
        return getString(date, DATETIME_PATTERN);
    }
 
    public static String getDateTimeZhString(Date date) {
        return getString(date, DATETIME_PATTERN_ZH);
    }
 
    public static String getString(Date date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }
 
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        return hour;
    }
 
    /**
     * 是否正在清空历史趋势图数据时间(8:10-8:25),是返回true,否则返回false
     *
     * @return
     */
    public static boolean isClearTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int mins = calendar.get(Calendar.MINUTE);
        if (8 == hour && (mins >= 10 && mins <= 25)) {
            return true;
        }
        return false;
    }
 
 
    /**
     * 是否在告警时间段,是返回true,否则返回false
     *
     * @param cronStr cron表达式
     * @return
     */
    public static boolean isWarnTime(String cronStr) {
        boolean sign = true;
        if (StringUtils.isEmpty(cronStr)) {
            return sign;
        }
        try {
            CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
            CronParser parser = new CronParser(cronDefinition);
            // "* * 8-20 ? * MON-FRI",不含双引号,表示星期一到星期五的8点-20点执行告警
            // "* * 8-20 * * ?",不含双引号,表示每天的8点-20点执行告警
            ExecutionTime executionTime = ExecutionTime.forCron(parser.parse(cronStr.trim()));
            Date date = new Date();
            int year = date.getYear() + 1900;
            int month = date.getMonth() + 1;
            int day = date.getDate();
            int hours = date.getHours();
            int minutes = date.getMinutes();
            int seconds = date.getSeconds();
            LocalDateTime ldt = LocalDateTime.of(year, month, day, hours, minutes, seconds);
            ZonedDateTime zbj = ldt.atZone(ZoneId.systemDefault());
 
            sign = executionTime.isMatch(zbj);
        } catch (Exception e) {
            logger.error("解析告警时间段表达式错误", e);
        }
        return sign;
    }
 
 
    /**
     * 从当前时间往前推几天,得到新时间
     *
     * @param day
     * @return
     */
    public static String getDateBefore(int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return getString(now.getTime(), DATETIME_PATTERN);
    }
 
    /**
     * 秒转换为指定格式的日期
     *
     * @param second
     * @param patten
     * @return
     */
    public static String secondToDate(Long second, String patten) {
        if (second == null) {
            return "";
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(second * 1000);//转换为毫秒
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(patten);
        String dateString = format.format(date);
        return dateString;
    }
 
    /**
     * 毫秒转换为指定格式的日期
     *
     * @param millisStr
     * @param patten
     * @return
     */
    public static String millisToDate(String millisStr, String patten) {
        if (StringUtils.isEmpty(millisStr)) {
            return "";
        }
        try {
            Long millis = Long.valueOf(millisStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(millis);//转换为毫秒
            Date date = calendar.getTime();
            SimpleDateFormat format = new SimpleDateFormat(patten);
            String dateString = format.format(date);
            return dateString;
        } catch (Exception e) {
            logger.error("millisToDate错误", e);
        }
        return "";
    }
 
    /**
     * 获取某几个小时之前的时间
     *
     * @param hours
     */
    public static String beforeHourToNowDate(Integer hours) {
        if (null == hours || 0 == hours) {
            hours = 1;
        }
        Calendar c = Calendar.getInstance();
        long diffTimes = 3600 * 1000;
        long nowTimes = c.getTimeInMillis();
        c.setTimeInMillis(nowTimes - (hours * diffTimes));
        SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_PATTERN);
        return sdf.format(c.getTime());
    }
 
    /**
     * 获取当前系统时间.
     * 格式yyyyMMddhhmmss
     *
     * @return 当前系统时间,不带符号
     */
    public static String getCurrentDateTimeNoChar() {
        String currentTime = getCurrentDateTime();
        if (!StringUtils.isEmpty(currentTime)) {
            currentTime = currentTime.replace("-", "").replace(" ", "").replace(":", "");
        }
        return currentTime;
    }
 
}