jdk1.8LocalDate与LocalDateTime工具类
public class DateUtils {
private static ThreadLocal<SimpleDateFormat> simpleDateFormatThreadLocal = ThreadLocal
.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
public static String format(Date date) {
return simpleDateFormatThreadLocal.get().format(date);
}
public static String getNow() {
return format(new Date());
}
public static Date parse(String dateString) throws ParseException {
return simpleDateFormat().parse(dateString);
}
public static final String DATEFORMAT1 = "yyyy-MM-dd HH:mm:ss";
public static final String DATEFORMAT2 = "yyyy-MM-dd HH:mm";
public static final String DATEFORMAT3 = "yyyy-MM-dd";
/**
* 获取隔日日期(时间类型不同)
*
* 比如今天是2019-02-01,调用这个方法会返回2019-02-02
*
* @return
*/
public static Date alternativeDateTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, 1);
return cal.getTime();
}
/**
* 获取隔日日期(时间类型不同)
*
* 比如今天是2019-02-02,调用这个方法会返回2019-02-01
*
* @return
*/
public static Date alternativeBeforeDateTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, -1);
return cal.getTime();
}
/**
* Date计算两个时间相差几天
* 备注:跨月的测试过,在DateUtilsTest类中
* @param startDate
* @param endDate
* @return 正数,负数
*/
public static long timeIntervalInDays(Date startDate, Date endDate) {
return timeIntervalInDayTimes(date2LocalDateTime(startDate), date2LocalDateTime(endDate));
}
/**
* 返回当前日期是星期几
* 用org.apache.commons.lang3.time.DateFormatUtils工具类
* @param date
* @return
*/
public static String getWeekOfDate(Date date) {
return DateFormatUtils.format(date, "E");
}
/**
* 获取两个日期之间的所有日期
* 备注:包含开始和结束时间
* @param begin
* @param end
* @return
*/
public static List<Date> getBetweenDate(Date begin, Date end) {
List<Date> result = new ArrayList<Date>();
//用Calendar 进行日期比较判断
Calendar calendar = Calendar.getInstance();
while (begin.getTime() <= end.getTime()) {
// 把日期添加到集合
result.add(begin);
// 设置日期
calendar.setTime(begin);
//把日期增加一天
calendar.add(Calendar.DAY_OF_YEAR, 1);
// 获取增加后的日期
begin = calendar.getTime();// NOSONAR
}
return result;
}
private static SimpleDateFormat simpleDateFormat() {
return simpleDateFormatThreadLocal.get();
}
private static long timeIntervalInDayTimes(LocalDateTime startDateTime, LocalDateTime endDateTime) {
return Duration.between(startDateTime, endDateTime).toDays();
}
private static Date localDateTime2Date(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return Date.from(instant);
}
private static LocalDateTime date2LocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
public static LocalDateTime strToLocalDateTime(String date, String pattern) {
DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
return LocalDateTime.parse(date, df);
}
/**
* 判断两个时间是否相隔day天
*/
public static boolean isIntervalDays(Date startTime, Date endTime, Integer day) {
long between = endTime.getTime() - startTime.getTime();
if (between > 24L * 3600000 * day) {
return true;
}
return false;
}
}