jdk1.8LocalDate工具类

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;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值