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;
|
}
|
|
}
|