下面这个是自己封装的DateUtil的工具类,主要是觉得有一些方法比较实用 封装了一下,有用到的可以直接复制一下代码。。。
package com.xindong.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@Service
@Slf4j
public class DateUtils {
public static final String DEFAULT_DATE = "EEE MMM dd HH:mm:ss zzz yyyy";
public static final String GENERAL_DATE = "yyyy-MM-dd HH:mm:ss";
public static final String COMMON_DATE = "yyyy年MM月dd日 HH时mm分ss秒";
public static Date stringToDate(String dateStr, String formatStr) throws Exception {
Date date = new Date();
try {
if (StringUtils.isEmpty(dateStr)) {
return null;
}
if (StringUtils.isEmpty(formatStr)) {
formatStr = DateUtils.GENERAL_DATE;
}
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
date = sdf.parse(dateStr);
} catch (Exception e) {
log.error(String.format("字符串转换指定日期错误--错误的字符串为 %s --异常为 %s",
dateStr, e.getMessage()));
throw e;
}
return date;
}
public static String dateToStr(Date date, String formatStr) throws Exception {
String dateStr = null;
try {
if (date == null) {
return null;
}
if (formatStr == null) {
formatStr = DateUtils.GENERAL_DATE;
}
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
dateStr = sdf.format(date);
} catch (Exception e) {
log.error(String.format("日期转化字符串错误--错误的日期为 %s --异常为 %s",
date, e.getMessage()));
throw e;
}
return dateStr;
}
public static String getCurrDate(String scheme) {
return DateFormatUtils.format(new Date(), scheme);
}
public static Date getStartTime(Date date) {
Calendar todayStart = Calendar.getInstance();
todayStart.setTime(date);
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
return todayStart.getTime();
}
public static Date getEndTime(Date date) {
Calendar todayEnd = Calendar.getInstance();
todayEnd.setTime(date);
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
return todayEnd.getTime();
}
public static String getAfterTime(int afterDay) throws Exception {
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
calendar.add(Calendar.DATE, afterDay);
return dateToStr(calendar.getTime(), null);
}
public static boolean compareDate(Date date1, Date date2) throws Exception {
if (date1 == null || date2 == null) {
log.error(String.format("填入的日期不能为空"));
throw new Exception("填入的日期不能为空");
}
return date1.before(date2);
}
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
public static boolean leapYear(int year) {
return year % 4 == 0 ? year % 100 == 0 ? year % 400 == 0 ? true : false : true : false;
}
}