public class DateUtil {
private static final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* @return 当天的起始时间点
*/
public static LocalDateTime initTimeOfCurrentDay() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
}
/**
* 当天的 23:59:59
* @return
*/
public static LocalDateTime lastTimeOfCurrentDay() {
return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
}
/**
*
* @param localDateTime
* @return 指定时间点那天的零点
*/
public static LocalDateTime initTimeOfDay(LocalDateTime localDateTime) {
return LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);
}
/**
*
* @param localDateTime
* @return 指定时间点那天的 23:59:59
*/
public static LocalDateTime lastTimeOfDay(LocalDateTime localDateTime) {
return LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MAX);
}
/**
* @param localDate 日期
* @return 指定日期的起始时间点
*/
public static LocalDateTime initTimeOfDay(LocalDate localDate) {
return LocalDateTime.of(localDate, LocalTime.MIN);
}
/**
* @param localDate 日期
* @return 指定日期的最后时间点
*/
public static LocalDateTime lastTimeOfDay(LocalDate localDate) {
return LocalDateTime.of(localDate, LocalTime.MAX);
}
/**
* LocalDateTime 转换成 java.util.Date
*
* @param localDateTime 新时间
* @return 旧时间
*/
public static Date localDateTime2Date(LocalDateTime localDateTime) {
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
return Date.from(instant);
}
/**
* java.util.Date 转换成 LocalDateTime
*
* @param date 旧时间
* @return 新时间
*/
public static LocalDateTime date2LocalDateTime(Date date) {
Instant instant = Instant.ofEpochMilli(date.getTime());
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
return localDateTime;
}
/**
* 毫秒时间戳转换成 LocalDateTime
*
* @param timeStampMill unxi timestamp millisecond
* @return 时间
*/
public static LocalDateTime millTimeStamp2LocalDateTime(Long timeStampMill) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStampMill), ZoneId.systemDefault());
}
/**
* 秒时间戳转换成 LocalDateTime
*
* @param timeStampSecond
* @return
*/
public static LocalDateTime secondTimeStamp2LocalDateTime(Long timeStampSecond) {
return LocalDateTime.ofInstant(Instant.ofEpochSecond(timeStampSecond), ZoneId.systemDefault());
}
/**
* 秒时间戳 转成 LocalDate
*
* @param timeStampSecond
* @return
*/
public static LocalDate timeStamp2LocalDate(Long timeStampSecond) {
ZonedDateTime zdt = Instant.ofEpochSecond(timeStampSecond).atZone(ZoneId.systemDefault());
return zdt.toLocalDate();
}
/**
* @param localDateTime 待格式化的日期
* @param pattern 日期格式 yyyy-MM-dd HH:mm:ss
* @return 格式化后的日期
*/
public static String formatDate(LocalDateTime localDateTime, String pattern) {
assert localDateTime != null;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return localDateTime.format(formatter);
}
public static Date formatDate(String date, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
//thread safe
LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
return localDateTime2Date(localDateTime);
}
public static String formatDate(Long timeStampSecond, String pattern) {
LocalDateTime localDateTime = secondTimeStamp2LocalDateTime(timeStampSecond);
//"yyyy-MM-dd HH:mm:ss"
return formatDate(localDateTime, pattern);
}
public static LocalDateTime formatLocalDateTime(String date, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return LocalDateTime.parse(date, formatter);
}
public static LocalDate formatLocalDate(String date, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return LocalDate.parse(date, formatter);
}
public static LocalTime formatLocalTime(String time, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return LocalTime.parse(time, formatter);
}
/**
* Date 类型的对象 转换 成字符串日期格式
*
* @param date Date 类型的对象
* @param pattern "yyyy-MM-dd HH:mm:ss"
* @return
*/
public static String date2String(Date date, String pattern) {
LocalDateTime tmp = date2LocalDateTime(date);
return formatDate(tmp, pattern);
}
/**
* 将 LocalDateTime 对象转化成 Long unix 时间戳
*
* @param localDateTime
* @return
*/
public static Long localDateTime2Long(LocalDateTime localDateTime) {
return localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond();
}
public static String localDateTime2String(LocalDateTime localDateTime) {
return df.format(localDateTime);
}
/**
*
* @param localDate
* @param pattern yyyy-MM-dd 或者 yyyyMMdd
* @return
*/
public static String localDate2String(LocalDate localDate, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return localDate.format(formatter);
}
}