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