JDK8 日期转换工具

package com.common.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * @ClassName: DateUtil
 * @Description: TODO(日期时间处理工具类)
 * @author yan
 * @date 2018年11月7日 上午11:30:46
 *
 */
public class DateUtil {
	/**
	 * 
	 * @Title: getDateTimeAsString
	 * @Description: TODO(将LocalDateTime转为自定义的时间格式的字符串)
	 * @param localDateTime
	 * @param format
	 * @return String
	 * @date 2018年11月7日 上午11:30:31
	 *
	 */
	public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
		return localDateTime.format(formatter);
	}

	/**
	 * 
	 * @Title: getDateTimeOfTimestamp
	 * @Description: TODO(将long类型的timestamp转为LocalDateTime)
	 * @param timestamp
	 * @return LocalDateTime
	 * @date 2018年11月7日 上午11:31:13
	 *
	 */
	public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
		Instant instant = Instant.ofEpochMilli(timestamp);
		ZoneId zone = ZoneId.systemDefault();
		return LocalDateTime.ofInstant(instant, zone);
	}

	/**
	 * 
	 * @Title: getDateTimeStrOfTimestamp
	 * @Description: TODO(将timestamp转为自定义的时间格式的字符串)
	 * @param timestamp
	 * @param format
	 * @return String
	 * @date 2018年11月7日 上午11:34:33
	 *
	 */
	public static String getDateTimeStrOfTimestamp(long timestamp, String format) {
		if (StringUtils.isBlank(format)) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		return getDateTimeAsString(getDateTimeOfTimestamp(timestamp), format);
	}

	/**
	 * 
	 * @Title: getTimestampOfDateTime
	 * @Description: TODO(将LocalDateTime转为long类型的timestamp)
	 * @param localDateTime
	 * @return long
	 * @date 2018年11月7日 上午11:31:21
	 *
	 */
	public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return instant.toEpochMilli();
	}

	/**
	 * 
	 * @Title: parseStringToDateTime
	 * @Description: TODO(将某时间字符串转为自定义时间格式的LocalDateTime)
	 * @param time
	 * @param format
	 * @return LocalDateTime
	 * @date 2018年11月7日 上午11:31:28
	 *
	 */
	public static LocalDateTime parseStringToDateTime(String time, String format) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
		return LocalDateTime.parse(time, df);
	}

	/**
	 * string时间转换unix时间戳
	 * @param time			String时间
	 * @param format		时间格式
	 * @return				unix时间戳
	 */
	public static long parseStringToUnixTime(String time,String format){
		LocalDateTime t = parseStringToDateTime(time,format);
		return getTimestampOfDateTime(t);
	}
	/**
	 * 
	    * @Title: parseStringToDate
	    * @Description: TODO(将某时间字符串转为自定义时间格式的LocalDate)
	    * @param date
	    * @param format
	    * @return
	   		LocalDate
		* @date 2018年11月8日 上午10:54:13
	    *
	 */
	public static LocalDate parseStringToDate(String date, String format) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
		return LocalDate.parse(date, df);
	}

	/**
	 * 计算当前日期与{@code endDate}的间隔天数
	 *
	 * @param endDate
	 * @return 间隔天数
	 */
	public static long intervalDay(LocalDate endDate) {
		return LocalDate.now().until(endDate, ChronoUnit.DAYS);
	}

	/**
	 * 计算日期{@code startDate}与{@code endDate}的间隔天数
	 *
	 * @param startDate
	 * @param endDate
	 * @return 间隔天数
	 */
	public static long intervalDay(LocalDate startDate, LocalDate endDate) {
		return startDate.until(endDate, ChronoUnit.DAYS);
	}

	/**
	 * 
	 * @Title: intervalMin
	 * @Description: TODO(计算当前日期与{@code timestamp}的间隔分钟)
	 * @param timestamp
	 * @return long
	 * @date 2018年11月7日 上午11:43:47
	 *
	 */
	public static long intervalMinutes(long timestamp) {
		return getDateTimeOfTimestamp(timestamp).until(LocalDateTime.now(), ChronoUnit.MINUTES);
	}

	/**
	 * 
	 * @Title: intervalSeconds
	 * @Description: TODO(计算当前日期与{@code timestamp}的间隔秒)
	 * @param timestamp
	 * @return long
	 * @date 2018年11月7日 上午11:55:07
	 *
	 */
	public static long intervalSeconds(long timestamp) {
		return getDateTimeOfTimestamp(timestamp).until(LocalDateTime.now(), ChronoUnit.SECONDS);
	}
	/**
	 * 
	    * @Title: dateAddOrSubtractDay
	    * @Description: TODO(在{@code time}上加或减{@code day}天)
	    * @param time
	    * @param day
	    * @return
	   		LocalDateTime
		* @date 2018年11月8日 上午9:58:24
	    *
	 */
	public static LocalDateTime dateAddOrSubtractDay(LocalDateTime time, int day) {
		return time.plus(Period.ofDays(day));
	}
	/**
	 * 
	    * @Title: currentDateAddOrSubtractDay
	    * @Description: TODO(在当前日期上加或减{@code day}天)
	    * @param day
	    * @return
	   		LocalDateTime
		* @date 2018年11月8日 上午10:02:36
	    *
	 */
	public static LocalDateTime currentDateAddOrSubtractDay(int day) {
		return LocalDateTime.now().plus(Period.ofDays(day));
	}
	
	public static void main(String[] args) {
		System.out.println(DateUtil.getDateTimeAsString(LocalDateTime.now(), "yyyy-MM-dd HH:mm EEEE"));
//		LocalDateTime date = parseStringToDateTime("2018-11-08", "yyyy-MM-dd");
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		LocalDate date = LocalDate.parse("2018-11-08", df);
		LocalDateTime datetime = LocalDateTime.of(date, LocalTime.of(0, 0, 0));
		long timestamp = getTimestampOfDateTime(datetime);
		long timestamp2 = getTimestampOfDateTime(LocalDateTime.now());
		System.out.println(timestamp);
		System.out.println(timestamp2);
		System.out.println(System.currentTimeMillis());
	}
	
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yn00

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值