import org.springframework.util.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* @ClassName DateUtil
* @description:
* @author: qsong
* @create: 2021-02-18 16:15
* @Version 1.0
**/
public class DateUtil {
/**
* 时间格式化
*/
public static String formatDate(Date time, String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
if (null != time) {
return df.format(time);
} else {
return "";
}
}
/**
* @param date 字符型时间格式
* */
public static Date strFormatDate(String date, String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 时间字符串格式化
*/
public static String formatStrDate(String dateStr, String beforeFormat, String afterFormat) {
try {
if (dateStr.length() != beforeFormat.length()) {
return dateStr;
}
SimpleDateFormat bdf = new SimpleDateFormat(beforeFormat);
SimpleDateFormat adf = new SimpleDateFormat(afterFormat);
if (null != dateStr) {
return adf.format(bdf.parse(dateStr));
}
} catch (Exception e) {
e.printStackTrace();
}
return dateStr;
}
/**
* 获取当前时间的字符串(yyyyMMddHHmmss)
* @return
*/
public static String getTimestampStr(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new Date());
}
/**
* 日切日期减1
* @param dailySwthchDate
* @return
*/
public static String convertDate(String dailySwthchDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar ordcal = Calendar.getInstance();
String returnDate = null;
try {
Date orderDate = sdf.parse(dailySwthchDate);
ordcal.setTime(orderDate);
} catch (Exception e) {
// 如果转化失败取当天时间
ordcal.setTime(new Date());
}finally{
ordcal.add(Calendar.DATE, -1);
returnDate = sdf.format(ordcal.getTime());
}
return returnDate;
}
//日期减一
public static Integer lastDate(Integer date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date orderDate = sdf.parse(String.valueOf(date));
Calendar ordcal = Calendar.getInstance();
ordcal.setTime(orderDate);
ordcal.add(Calendar.DATE, -1);
return Integer.valueOf(sdf.format(ordcal.getTime()));
} catch (Exception e) {
sdf = new SimpleDateFormat("yyyyMMdd");
return Integer.valueOf(sdf.format(new Date()));
}
}
/**
* 日切日期加一减一天
* @param dailySwthchDate
* @return
*/
public static String compaterDailyDate(String dailySwthchDate,int dayDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
String returnDate = null;
try {
Date switchDate = sdf.parse(dailySwthchDate);
calendar.setTime(switchDate);
} catch (ParseException e) {
// 如果转化失败取当天时间
calendar.setTime(new Date());
} finally {
calendar.add(Calendar.DATE, dayDate);
returnDate = sdf.format(calendar.getTime());
}
return returnDate;
}
/**
* 上日
*/
public static String formatDate(String sDate,String format){
if(StringUtils.isEmpty(sDate)){
return "";
}
SimpleDateFormat df = new SimpleDateFormat(format);
Calendar cal = null;
try {
Date dDate = df.parse(sDate);
cal = Calendar.getInstance();
cal.setTime(dDate);
cal.add(Calendar.DAY_OF_MONTH, -1);
} catch (ParseException e) {
e.printStackTrace();
}
return df.format(cal.getTime());
}
/**
* 下日
*/
public static String formatNextDate(String sDate,String format){
if(StringUtils.isEmpty(sDate)){
return "";
}
SimpleDateFormat df = new SimpleDateFormat(format);
Calendar cal = null;
try {
Date dDate = df.parse(sDate);
cal = Calendar.getInstance();
cal.setTime(dDate);
cal.add(Calendar.DAY_OF_MONTH, 1);
} catch (ParseException e) {
e.printStackTrace();
}
return df.format(cal.getTime());
}
public static Date formatStr2Date(String sDate,String format){
if(StringUtils.isEmpty(sDate)){
return null;
}
SimpleDateFormat df = new SimpleDateFormat(format);
Calendar cal = null;
try {
Date dDate = df.parse(sDate);
cal = Calendar.getInstance();
cal.setTime(dDate);
} catch (ParseException e) {
e.printStackTrace();
}
return cal.getTime();
}
/**
* 获取上一日日期(yyyy-MM-dd)
* @return
*/
public static String getRoundDateString(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date date = calendar.getTime();
return sdf.format(date);
}
public static String nextDate(String date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date orderDate = sdf.parse(date);
Calendar ordcal = Calendar.getInstance();
ordcal.setTime(orderDate);
ordcal.add(Calendar.DATE, 1);
return sdf.format(ordcal.getTime());
} catch (Exception e) {
return getTimestampStr();
}
}
public static String expDate(String expDate){
String date=null;
try {
if(!StringUtils.isEmpty(expDate)){
SimpleDateFormat sdf = new SimpleDateFormat("yymm");
Date dateStr=sdf.parse(expDate);
sdf = new SimpleDateFormat("mmyy");
date=sdf.format(dateStr);
}
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
public boolean computeIntervalTime(Integer intervalTime, Date finishTime) {
if (finishTime == null || intervalTime == null || intervalTime <= 0) {
return false;
}
Date currentDate = new Date();
long nextTime = currentDate.getTime() / 60000;
long parentTime = finishTime.getTime() / 60000;
return nextTime - parentTime < intervalTime;
}
/**
* 计算毫秒数
* @MethodName: timeMillis
* @param @param hour
* @param @param minute
*/
private static long timeMillis(int hour, int minute) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
return calendar.getTimeInMillis();
}
/**
* 判断当前时间是否在一定的时间范围内
* @param startTime
* @return boolean
*/
public static boolean isInBetweenTimes(String startTime, String endTime) {
Date nowTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String time = sdf.format(nowTime);
if (time.compareTo(startTime) >= 0 && time.compareTo(endTime) <= 0) {
return true;
} else {
return false;
}
}
/**
* 判断开始时间和结束时间,是否超出了当前时间的一定的间隔数限制, 时间单位默认为天数 如:开始时间和结束时间,不能超出距离当前时间90天
* @param startDate
* 开始时间
* @param endDate
* 结束时间按
* @param interval
* 间隔数
* @return
*/
public static boolean isOverIntervalLimit(Date startDate, Date endDate, int interval) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_MONTH, interval * (-1));
Date curDate = getDayStart(cal.getTime());
if (getDayStart(startDate).compareTo(curDate) < 0 || getDayStart(endDate).compareTo(curDate) < 0) {
return true;
}
return false;
}
/**
* 得到day的起始时间点。
* @param date
* @return
*/
public static Date getDayStart(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 得到day的终止时间点.
* @param date
* @return
*/
public static Date getDayEnd(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.MILLISECOND, -1);
return calendar.getTime();
}
/**
* 计算 second 秒后的时间
* @param date
* @param second
* @return
*/
public static Date addSecond(Date date, int second) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
;
calendar.add(Calendar.SECOND, second);
return calendar.getTime();
}
/**
* 计算 minute 分钟后的时间
* @param date
* @param minute
* @return
*/
public static Date addMinute(Date date, int minute) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, minute);
return calendar.getTime();
}
/**
* 计算 hour 小时后的时间
* @param date
* @param hour
* @return
*/
public static Date addHour(Date date, int hour) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, hour);
return calendar.getTime();
}
/**
* 计算 day 天后的时间
* @param date
* @param day
* @return
*/
public static Date addDay(Date date, int day) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, day);
return calendar.getTime();
}
/**
* 字符转日期
* @param dateStr
* @return
*/
public static Date getDateByStr(String dateStr) {
SimpleDateFormat formatter = null;
if (dateStr == null) {
return null;
} else if (dateStr.length() == 10) {
formatter = new SimpleDateFormat("yyyy-MM-dd");
} else if (dateStr.length() == 16) {
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
} else if (dateStr.length() == 19) {
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} else if (dateStr.length() > 19) {
dateStr = dateStr.substring(0, 19);
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} else {
return null;
}
try {
return formatter.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 是否为时间格式:hh:mm:ss
* @param timeStr
* @return
*/
public static boolean isTime(String timeStr) {
SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss");
Date date = null;
try {
date = df.parse(timeStr);
} catch (ParseException e) {
return false;
}
return date != null;
}
/**
* 是否为日期时间格式:yyyy-MM-dd hh:mm:ss or yyyy-MM-dd hh:mm
* @param dateTime
* @return
*/
public static boolean isDateTime(String dateTime) {
int first = dateTime.indexOf(":");
int last = dateTime.lastIndexOf(":");
if (first == -1) {
return false;
}
SimpleDateFormat df = null;
if (first == last) {
df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
} else {
df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
}
Date date = null;
try {
date = df.parse(dateTime);
} catch (ParseException e) {
return false;
}
return date == null;
}
/**
* 时间比较
* @param date1
* @param date2
* @return DATE1>DATE2返回1,DATE1<DATE2返回-1,等于返回0
*/
public static int compareDate(String date1, String date2, String format) {
DateFormat df = new SimpleDateFormat(format);
try {
Date dt1 = df.parse(date1);
Date dt2 = df.parse(date2);
if (dt1.getTime() > dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
/**
* 给时间加上几个小时
* @param day 当前时间 格式:yyyy-MM-dd HH:mm:ss
* @param hour 需要加的时间
* @return
*/
public static String addDateHour(String day, int hour) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(day);
} catch (Exception ex) {
ex.printStackTrace();
}
if (date == null)
return "";
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, hour);// 24小时制
date = cal.getTime();
cal = null;
return format.format(date);
}
/**
* 获取指定分钟之前的时间
* @param minute 指定分钟
* @return 指定分钟之前格式化后的时间 yyyy-MM-dd HH:MM
* 保留,先,admin中
*/
public static String getDateOfSpecifyMinuteBeFore(Long minute) {
LocalDateTime today = LocalDateTime.now();
LocalDateTime after = today.minus(minute, ChronoUnit.MINUTES);
return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(after);
}
public static String compareDate(String date1, String date2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
try {
Date dt1 = df.parse(date1);
Date dt2 = df.parse(date2);
if (dt1.getTime() > dt2.getTime()) {
return date1;
} else if (dt1.getTime() < dt2.getTime()) {
return date2;
} else {
return date1;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return date1;
}
/**
* 获取两个时间相差分钟数
* @param start
* @param end
* @return 分钟数
*/
public static int getTimeDifference(String start,String end){
//把当前时间和要比较的时间转换为Date类型,目的在于得到这两个时间的毫秒值
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date parse = null;
Date now = null;
try {
parse = sdf.parse(start);
now = sdf.parse(end);
} catch (ParseException e) {
}
//获得这两个时间的毫秒值后进行处理(因为我的需求不需要处理时间大小,所以此处没有处理,可以判断一下哪个大就用哪个作为减数。)
long diff = now.getTime() - parse.getTime();
//此处用毫秒值除以分钟再除以毫秒既得两个时间相差的分钟数
long minute = diff/60/1000;
return (int)minute;
}
public static String simpleDatetime(String datetime) {
if (datetime == null) {
return "";
}
if (datetime.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) {
return datetime.substring(5, 16);
} else if (datetime.matches("\\d{4}-\\d{2}-\\d{2}")) {
return datetime.substring(5, 10);
} else if (datetime.matches("\\d{2}:\\d{2}:\\d{2}")) {
return datetime.substring(0, 5);
}
return datetime;
}
// 判断是否在可用时间内
public static long DEFAULT_TIMEOUT_SECOND = 70 * 60 * 1000;
public static boolean isUsable(Date date) {
if (null == date) {
return false;
}
if (new Date().getTime() - date.getTime() > DEFAULT_TIMEOUT_SECOND) {
return false;
}
return true;
}
/**
* 获取前一天0点时间
* @param date
* @return
*/
public static Date getStartOfYesterday(Date date) {
long zero = System.currentTimeMillis() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
zero = zero - 1000 * 3600 * 24;
return new Date(zero);
}
/**
* 获取前一天23:59
* @param date
* @return
*/
public static Date getEndOfYesterday(Date date) {
long zero = getStartOfYesterday(new Date()).getTime();
return new Date(zero + 1000 * 3600 * 24 - 1);
}
public static void main(String[] args) {
//获取10分钟之前的时间
String specifyMinutesDate = DateUtil.getDateOfSpecifyMinuteBeFore(10L);
System.out.println(specifyMinutesDate);
Date now = new Date();
Date date=DateUtil.getStartOfYesterday(now);
System.out.println(date);
}
}
日期处理工具类
最新推荐文章于 2021-02-24 11:43:47 发布