public class DateUtils {
private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);
// 将 Date 类型的时间格式化为 String 类型 (例如:将 Fri Dec 31 14:17:04 CST 2021 转化为 2021-12-31 14:17:04)
public static String formatDateToString(Date date, String format) {
try {
if (Objects.nonNull(date)) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
} catch (Exception e) {
logger.error("formatDate error, the reason is : {}", e);
}
return "";
}
// 将 String 类型的时间解析为 Date 类型 (例如:将 2021-12-31 15:18:07 转化为 Fri Dec 31 15:18:07 CST 2021)
public static Date parseDateToString(String dateStr, String format) {
try {
if (StringUtils.isNotBlank(dateStr)) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(dateStr);
}
} catch (ParseException e) {
logger.error("parseDateFromString error, the reason is : {}", e);
}
return null;
}
// 将 Date 类型的时间格式化为 String 类型,附带毫秒值 (例如:将 Fri Dec 31 15:25:41 CST 2021 转化为 2021-12-31 15:25:41.669)
public static String formatDateToStringWithMills(Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
String dateStr = simpleDateFormat.format(date);
return dateStr;
}
// 将一种格式的字符串时间转化为另外一种格式的字符串时间 (例如:将 yyyy-MM-dd HH:mm:ss 格式的时间转化为 yyyy-MM-dd)
public static String getDateStr(String dateStr, String format1, String format2) throws ParseException {
SimpleDateFormat sdf1 = new SimpleDateFormat(format1);
Date date = sdf1.parse(dateStr);
SimpleDateFormat sdf2 = new SimpleDateFormat(format2);
return sdf2.format(date);
}
// 将 yyyy-MM-dd HH:mm:ss 格式的时间转化为秒级时间戳 (例如:将 2021-12-31 15:57:53 转化为 1640937473)
public static Long getTimestampFromDateStr(String dateStr) {
return Timestamp.valueOf(dateStr).getTime() / 1000;
}
// 将秒级时间戳转化为 yyyy-MM-dd HH:mm:ss 格式时间 (例如:将 2021-12-31 15:57:53 转化为 1640937473)
public static String getDateStrFromSecondTimestamp(Long secondTimestamp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(secondTimestamp * 1000);
return sdf.format(date);
}
// 通过两个时间字符串比较时间的先后
public static int compare_date(String dateStr1, String dateStr2, String format) {
DateFormat df = new SimpleDateFormat(format);
try {
Date date1 = df.parse(dateStr1);
Date date2 = df.parse(dateStr2);
if (date1.getTime() > date2.getTime()) {
System.out.println("dateStr1 在 dateStr2 后");
return 1;
} else if (date1.getTime() < date2.getTime()) {
System.out.println("dateStr1 在 dateStr2 前");
return -1;
} else {
System.out.println("时间相等");
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
// 获取传入时间的当天开始时间(例如传入 2019-08-29 12:23:23 返回 2019-08-29 00:00:00)
public static String getDateStrBegin(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return sdf.format(calendar.getTime());
}
// 获取传入时间的当天结束时间 (例如传入 2019-08-29 12:23:23 返回 2019-08-29 23:59:59)
public static String getDateStrEnd(Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return simpleDateFormat.format(calendar.getTime());
}
// 获取指定时间与当前时间的差值,单位 s (例如:指定时间为 2021-12-31 16:01:00 当前时间为 2021-12-31 16:00:00,那么他们的差值为 60 s)
public static Long getMiol() {
Calendar ca = Calendar.getInstance();
// 指定时间(毫秒)
ca.set(Calendar.HOUR_OF_DAY, 16);
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
long assignTime = ca.getTimeInMillis() / 1000;
// 当前时间(毫秒)
long currentTime = (System.currentTimeMillis()) / 1000;
long different = assignTime - currentTime;
return different;
}
// 获取当天的开始时间
public static Date getDayBegin() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
// 获取当天的结束时间
public static Date getDayEnd() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999);
return cal.getTime();
}
// 获取昨天的开始时间
public static Date getBeginDayOfYesterday() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
// 获取昨天的结束时间
public static Date getEndDayOfYesterDay() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
// 获取本周的开始时间
public static Date getBeginDayOfWeek() {
Date date = new Date();
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
return getDayStartTime(cal.getTime());
}
// 获取本周的结束时间
public static Date getEndDayOfWeek() {
Calendar cal = Calendar.getInstance();
cal.setTime(getBeginDayOfWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
}
// 获取上周的开始时间
public static Date getBeginDayOfLastWeek() {
Date date = new Date();
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek - 7);
return getDayStartTime(cal.getTime());
}
// 获取上周的结束时间
public static Date getEndDayOfLastWeek() {
Calendar cal = Calendar.getInstance();
cal.setTime(getBeginDayOfLastWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
}
// 获取本月的开始时间
public static Date getBeginDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
return getDayStartTime(calendar.getTime());
}
// 获取本月的结束时间
public static Date getEndDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth() - 1, day);
return getDayEndTime(calendar.getTime());
}
// 获取上月的开始时间
public static Date getBeginDayOfLastMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 2, 1);
return getDayStartTime(calendar.getTime());
}
// 获取上月的结束时间
public static Date getEndDayOfLastMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 2, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth() - 2, day);
return getDayEndTime(calendar.getTime());
}
// 获取某个日期的开始时间
public static Timestamp getDayStartTime(Date date) {
Calendar calendar = Calendar.getInstance();
if (null != date)
calendar.setTime(date);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0,
0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return new Timestamp(calendar.getTimeInMillis());
}
// 获取某个日期的结束时间
public static Timestamp getDayEndTime(Date date) {
Calendar calendar = Calendar.getInstance();
if (null != date)
calendar.setTime(date);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23,
59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return new Timestamp(calendar.getTimeInMillis());
}
// 获取今年是哪一年
public static Integer getNowYear() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return Integer.valueOf(gc.get(1));
}
// 获取本月是哪一月
public static int getNowMonth() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return gc.get(2) + 1;
}
// 两个日期相减得到的天数
public static int getDiffDays(Date beginDate, Date endDate) {
if (beginDate == null || endDate == null) {
throw new IllegalArgumentException("getDiffDays param is null!");
}
long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24);
int days = new Long(diff).intValue();
return days;
}
// 两个日期相减得到的毫秒数
public static long dateDiff(Date beginDate, Date endDate) {
long date1ms = beginDate.getTime();
long date2ms = endDate.getTime();
return date2ms - date1ms;
}
// 返回某个日期前几天的日期
public static Date getFrontDay(Date date, int i) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
return cal.getTime();
}
// 获取本月的前某个月的开始时间
public static Date getBeginDayOfBeforeMonth(int i) {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1 - i, 1);
return getDayStartTime(calendar.getTime());
}
// 获取本月的前某个月的结束时间
public static Date getEndDayOfBeforeMonth(int i) {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1 - i, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth() - 1 - i, day);
return getDayEndTime(calendar.getTime());
}
// 获取指定日期当月的第一天的开始时间
public static Date getFirstDayOfGivenMonth(Date date) {
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.MONTH, 0);
return getDayStartTime(calendar.getTime());
} catch (Exception e) {
logger.error("getFirstDayOfGivenMonth error, the reason is : {}", e);
}
return null;
}
}