package com.zzg.common.utils;
|
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
import org.apache.commons.lang3.time.DateUtils;
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.List;
|
|
|
/**
|
* Description:日期工具类
|
*
|
* @author lihaipeng
|
* @create 2020-08-12
|
*/
|
public class DateUtil {
|
|
/**
|
* 仅显示年月日,例如 2015-08-11.
|
*/
|
public static final String DATE_FORMAT = "yyyy-MM-dd";
|
/**
|
* 显示年月日时分秒,例如 2015-08-11 09:51:53.
|
*/
|
public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
|
/**
|
* 仅显示时分秒,例如 09:51:53.
|
*/
|
public static final String TIME_FORMAT = "HH:mm:ss";
|
|
/**
|
* 每天的毫秒数 8640000.
|
*/
|
public static final long MILLISECONDS_PER_DAY = 86400000L;
|
|
/**
|
* 每周的天数.
|
*/
|
public static final long DAYS_PER_WEEK = 7L;
|
|
/**
|
* 每小时毫秒数.
|
*/
|
public static final long MILLISECONDS_PER_HOUR = 3600000L;
|
|
/**
|
* 每分钟秒数.
|
*/
|
public static final long SECONDS_PER_MINUTE = 60L;
|
|
/**
|
* 每小时秒数.
|
*/
|
public static final long SECONDS_PER_HOUR = 3600L;
|
|
/**
|
* 每天秒数.
|
*/
|
public static final long SECONDS_PER_DAY = 86400L;
|
|
/**
|
* 每个月秒数,默认每月30天.
|
*/
|
public static final long SECONDS_PER_MONTH = 2592000L;
|
|
/**
|
* 每年秒数,默认每年365天.
|
*/
|
public static final long SECONDS_PER_YEAR = 31536000L;
|
|
/**
|
* 常用的时间格式.
|
*/
|
public static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"};
|
|
/**
|
* 得到当前日期字符串.
|
*
|
* @return String 日期字符串,例如2015-08-11
|
* @since 1.0
|
*/
|
public static String getDateByParseOne() {
|
return getDate(parsePatterns[0]);
|
}
|
|
/**
|
* 获取当前时间指定格式下的字符串.
|
*
|
* @param pattern 转化后时间展示的格式,例如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"等
|
* @return String 格式转换之后的时间字符串.
|
* @since 1.0
|
*/
|
public static String getDate(String pattern) {
|
return DateFormatUtils.format(new Date(), pattern);
|
}
|
|
/**
|
* 获取日期时间字符串
|
*
|
* @param date 需要转化的日期时间
|
* @param pattern 时间格式,例如"yyyy-MM-dd" "HH:mm:ss" "E"等
|
* @return String 格式转换后的时间字符串
|
* @since 1.0
|
*/
|
public static String formatDate(Date date, String pattern) {
|
return DateFormatUtils.format(date, pattern);
|
}
|
|
/**
|
* 将日期型字符串转换为日期格式.
|
* 支持的日期字符串格式包括"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
|
* "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"
|
*
|
* @param dataStr
|
* @return Date
|
* @since 1.0
|
*/
|
public static Date formatDate(String dataStr, String pattern) {
|
if (dataStr == null) {
|
return null;
|
}
|
try {
|
return DateUtils.parseDate(dataStr, pattern);
|
} catch (ParseException e) {
|
return null;
|
}
|
}
|
|
/**
|
* 将日期型字符串转换为日期格式.
|
* 支持的日期字符串格式包括"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
|
* "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"
|
*
|
* @param str
|
* @return Date
|
* @since 1.0
|
*/
|
public static Date parseDate(Object str) {
|
if (str == null) {
|
return null;
|
}
|
try {
|
return DateUtils.parseDate(str.toString(), parsePatterns);
|
} catch (ParseException e) {
|
return null;
|
}
|
}
|
|
/**
|
* 计算两个日期之间相差天数.
|
*
|
* @param start 计算开始日期
|
* @param end 计算结束日期
|
* @return long 相隔天数
|
* @since 1.0
|
*/
|
public static long getDaysBetween(Date start, Date end) {
|
// 将指定日期转换为yyyy-MM-dd格式
|
start = DateUtil.parseDate(DateUtil.formatDate(start, DateUtil.DATE_FORMAT));
|
// 当前日期转换为yyyy-MM-dd格式
|
end = DateUtil.parseDate(DateUtil.formatDate(end, DateUtil.DATE_FORMAT));
|
long diff = 0;
|
if (null != start && null != end) {
|
diff = (end.getTime() - start.getTime()) / DateUtil.MILLISECONDS_PER_DAY;
|
}
|
return diff;
|
}
|
public static long getDaysBetween(String start, Date end) {
|
// 将指定日期转换为yyyy-MM-dd格式
|
Date startDate = parseDate(start);
|
// 当前日期转换为yyyy-MM-dd格式
|
end = DateUtil.parseDate(DateUtil.formatDate(end, DateUtil.DATE_FORMAT));
|
long diff = 0;
|
if (null != start && null != end) {
|
diff = (end.getTime() - startDate.getTime()) / DateUtil.MILLISECONDS_PER_DAY;
|
}
|
return diff;
|
}
|
|
/**
|
* 将时间戳转换为时间
|
*
|
* @param s
|
* @return
|
* @throws Exception
|
*/
|
public static String stampToTime(String s) {
|
String res;
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
long lt = new Long(s);
|
//将时间戳转换为时间
|
Date date = new Date(lt);
|
//将时间调整为yyyy-MM-dd HH:mm:ss时间样式
|
res = simpleDateFormat.format(date);
|
return res;
|
}
|
|
/**
|
* 将字符串时间转换为long类型时间戳
|
*/
|
|
public static long dateToStamp(String s, String pattern) throws ParseException {
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
|
Date date = simpleDateFormat.parse(s);
|
long ts = date.getTime();
|
return date.getTime();
|
}
|
|
/**
|
* 判断当前时间是否在指定时间5分钟内
|
*
|
* @param isDate
|
* @return
|
*/
|
public static boolean judgeDate(Date isDate) {
|
Calendar c1 = Calendar.getInstance();
|
Calendar c2 = Calendar.getInstance();
|
Calendar c3 = Calendar.getInstance();
|
c1.setTime(isDate);//要判断的日期
|
c2.setTime(new Date());//初始日期
|
c3.setTime(new Date());//也给初始日期 把分钟加五
|
c3.add(Calendar.MINUTE, 5);
|
c2.add(Calendar.MINUTE, -5);//减去五分钟
|
if (c1.after(c2) && c1.before(c3)) {
|
//5分钟内
|
return false;
|
} else {
|
//5分钟外
|
return true;
|
}
|
}
|
|
private static final SimpleDateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
|
private static final SimpleDateFormat YYYY_MM_DD_TO_CHINESE = new SimpleDateFormat("yyyy年MM月dd日");
|
|
/**
|
* 获得该月第一天
|
*
|
* @param year
|
* @param month
|
* @return
|
*/
|
public static String getFirstDayOfMonth(int year, int month) {
|
Calendar cal = Calendar.getInstance();
|
// 设置年份
|
cal.set(Calendar.YEAR, year);
|
// 设置月份
|
cal.set(Calendar.MONTH, month - 1);
|
// 获取某月最小天数
|
int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
|
// 设置日历中月份的最小天数
|
cal.set(Calendar.DAY_OF_MONTH, firstDay);
|
// 格式化日期
|
String firstDayOfMonth = YYYY_MM_DD.format(cal.getTime());
|
return firstDayOfMonth;
|
}
|
|
/**
|
* 获得该月最后一天
|
*
|
* @param year
|
* @param month
|
* @return
|
*/
|
public static String getLastDayOfMonth(int year, int month) {
|
Calendar cal = Calendar.getInstance();
|
// 设置年份
|
cal.set(Calendar.YEAR, year);
|
// 设置月份
|
cal.set(Calendar.MONTH, month - 1);
|
// 获取某月最大天数
|
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
// 设置日历中月份的最大天数
|
cal.set(Calendar.DAY_OF_MONTH, lastDay);
|
// 格式化日期
|
String lastDayOfMonth = YYYY_MM_DD.format(cal.getTime());
|
return lastDayOfMonth;
|
}
|
|
/**
|
* 日期转换成字符串(yyyy-MM-dd)
|
*
|
* @param date
|
* @return str
|
*/
|
public static String transDateToStr(Date date) {
|
String str = YYYY_MM_DD.format(date);
|
return str;
|
}
|
|
|
/**
|
* 日期转换成字符串(yyyy年MM月dd日)
|
*
|
* @param date
|
* @return str
|
*/
|
public static String transDateToChineseStr(Date date) {
|
return YYYY_MM_DD_TO_CHINESE.format(date);
|
}
|
|
|
/**
|
* 日期转换成字符串(自定义转换格式)
|
*
|
* @param date
|
* @return str
|
*/
|
public static String transDateToStr(String pattern, Date date) {
|
SimpleDateFormat format = new SimpleDateFormat(pattern);
|
return format.format(date);
|
}
|
|
/**
|
* 字符串转换成日期(yyyy-MM-dd)
|
*
|
* @param str
|
* @return date
|
*/
|
public static Date transStrToDate(String str) {
|
Date date = null;
|
try {
|
date = YYYY_MM_DD.parse(str);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return date;
|
}
|
|
/**
|
* 字符串转换成日期(自定义转换格式)
|
*
|
* @param pattern
|
* @param str
|
*/
|
public static Date transStrToDate(String pattern, String str) {
|
SimpleDateFormat format = new SimpleDateFormat(pattern);
|
Date date = null;
|
try {
|
date = format.parse(str);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return date;
|
}
|
|
/**
|
* 获取某日期区间的所有日期 日期倒序
|
*
|
* @param startDate 开始日期
|
* @param endDate 结束日期
|
* @param dateFormat 日期格式
|
* @return 区间内所有日期
|
*/
|
public static List<String> getPerDaysByStartAndEndDate(String startDate, String endDate, String dateFormat) {
|
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
|
try {
|
Date sDate = format.parse(startDate);
|
Date eDate = format.parse(endDate);
|
long start = sDate.getTime();
|
long end = eDate.getTime();
|
if (start > end) {
|
return null;
|
}
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(eDate);
|
List<String> res = new ArrayList<String>();
|
while (end >= start) {
|
res.add(format.format(calendar.getTime()));
|
calendar.add(Calendar.DAY_OF_MONTH, -1);
|
end = calendar.getTimeInMillis();
|
}
|
return res;
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 日期转星期
|
*
|
* @param datetime
|
* @return
|
*/
|
public static String dateToWeek(String datetime) {
|
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
|
// 获得一个日历
|
Calendar cal = Calendar.getInstance();
|
Date datet = null;
|
try {
|
datet = YYYY_MM_DD.parse(datetime);
|
cal.setTime(datet);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
// 指示一个星期中的某天。
|
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
|
if (w < 0) {
|
w = 0;
|
}
|
return weekDays[w];
|
}
|
|
/***
|
* 日期转换cron表达式
|
*
|
* convert Date to cron ,eg. "00 07 10 15 01 ? 2016"
|
*
|
* @param date
|
* : 时间点
|
* @return
|
*/
|
public static String getCron(Date date) {
|
SimpleDateFormat sdf = new SimpleDateFormat("ss mm HH dd MM ? yyyy");
|
String formatTimeStr = null;
|
if (date != null) {
|
formatTimeStr = sdf.format(date);
|
}
|
return formatTimeStr;
|
}
|
|
/**
|
* 计算两个日期相差几个月
|
*
|
* @param date1 <String>
|
* @param date2 <String>
|
* @return int
|
* @throws ParseException
|
*/
|
public static int getMonthSpace(String date1, String date2) throws ParseException {
|
|
int result = 0;
|
|
Calendar c1 = Calendar.getInstance();
|
Calendar c2 = Calendar.getInstance();
|
|
c1.setTime(YYYY_MM_DD.parse(date1));
|
c2.setTime(YYYY_MM_DD.parse(date2));
|
|
result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
|
|
return result == 0 ? 1 : Math.abs(result);
|
|
}
|
|
/**
|
* 计算两个日期相差天数
|
*
|
* @param early <Date>
|
* @param late <Date>
|
* @return
|
*/
|
public static final int daysBetween(Date early, Date late) {
|
Calendar calst = Calendar.getInstance();
|
Calendar caled = Calendar.getInstance();
|
calst.setTime(early);
|
caled.setTime(late);
|
// 设置时间为0时
|
calst.set(Calendar.HOUR_OF_DAY, 0);
|
calst.set(Calendar.MINUTE, 0);
|
calst.set(Calendar.SECOND, 0);
|
caled.set(Calendar.HOUR_OF_DAY, 0);
|
caled.set(Calendar.MINUTE, 0);
|
caled.set(Calendar.SECOND, 0);
|
// 得到两个日期相差的天数
|
int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
|
return days;
|
}
|
|
/**
|
* 获取当前时间年度
|
* @return
|
*/
|
public static String getNowYear(){
|
return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
|
}
|
|
/**
|
* 计算两个日期相差多少分钟
|
*
|
* @param early <Date>
|
* @param late <Date>
|
* @return
|
*/
|
public static final int minutesBetween(Date early, Date late){
|
/*Calendar calst = Calendar.getInstance();
|
Calendar caled = Calendar.getInstance();
|
calst.setTime(early);
|
caled.setTime(late);
|
return (int) ((caled.getTime().getTime() - calst.getTime().getTime())/1000/60);*/
|
return (daysBetween(early,late)+1)*24*60;
|
}
|
/**
|
* 获取当前日期,日期秒设置为0
|
* @return
|
*/
|
public static Date getNewDate(){
|
Date date = new Date();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
return calendar.getTime();
|
}
|
|
/**
|
* 计算与当前日期相差给定天数的日期,例如:differ = 1,则返回一天后的日期;differ = -1,则返回昨天的日期
|
*
|
* @param differ 天数
|
* @return
|
*/
|
public static Date calculateDate(int differ) {
|
Date date = new Date();
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.add(Calendar.DAY_OF_MONTH, differ);
|
date = calendar.getTime();
|
return date;
|
}
|
|
/**
|
* 计算与给定日期相差给定天数的日期,例如:differ = 1,则返回给定日期一天后的日期;differ = -1,则返回给定日期的一天前的日期
|
*
|
* @param differ 天数
|
* @return
|
*/
|
public static Date calculateDate(Date date, int differ) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.add(Calendar.DAY_OF_MONTH, differ);
|
date = calendar.getTime();
|
return date;
|
}
|
|
/**
|
* 日期格式化
|
* @param date
|
* @return
|
*/
|
public static String getYearByDate(Date date) {
|
try {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
int year = calendar.get(Calendar.YEAR);
|
return year + "";
|
} catch (Exception e) {
|
return "未知";
|
}
|
}
|
/**
|
* 加一分钟
|
*/
|
public static Date addMinutes(final Date date, int amount) {
|
return DateUtils.addMinutes(date, amount);
|
}
|
}
|