package com.boying.util;
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.sql.Timestamp;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
|
|
/**
|
* @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;
|
}
|
|
|
|
|
/**
|
* 从当前时间往前推几天,得到新时间
|
*
|
* @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;
|
}
|
|
}
|