Java 8 新的时间工具类
package com.df.dkcs.diversion.core.uitls;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class DateTimeUtils {
public static LocalDateTime convertDateToLDT(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
public static Date convertLDTToDate(LocalDateTime time) {
return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
}
public static Long getMilliByTime(LocalDateTime time) {
return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
public static Long getSecondsByTime(LocalDateTime time) {
return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
}
public static LocalDateTime getDayStart(LocalDateTime time) {
return time.withHour(0)
.withMinute(0)
.withSecond(0)
.withNano(0);
}
public static LocalDateTime getDayEnd(LocalDateTime time) {
return time.withHour(23)
.withMinute(59)
.withSecond(59)
.withNano(999999999);
}
public static Long getDayLaveSeconds(LocalDateTime time) {
return (getSecondsByTime(getDayEnd(time)) - getSecondsByTime(time));
}
public static Long getDayLaveMilli(LocalDateTime time) {
return (getMilliByTime(getDayEnd(time)) - getMilliByTime(time));
}
}