/**
* @Description 获取到凌晨之间到秒(当前时间:2019-10-01 09:09:09到2019-10-01 23:59:59)
*/
public static final long getBmas() {
int currenTime = LocalDateTime.now().get(ChronoField.SECOND_OF_DAY);
int maxTime = LocalDate.now().atTime(LocalTime.MAX).get(ChronoField.SECOND_OF_DAY);
long time = maxTime - currenTime;
return time;
}
/**
* 判断时间是否存在区间内
* @param nowTime
* @param beginTime
* @param endTime
*/
public static boolean timeInterva(LocalDateTime nowTime, LocalDateTime beginTime, LocalDateTime endTime) {
if (nowTime.isAfter(beginTime) && nowTime.isBefore(endTime)) {
return true;
}
return false;
}
//获取当前日期及时间:2019-11-21T09:09:09(除去毫秒)
String now = LocalDateTime.now().withNano(0).toString();
/**
* LocalDate转Date
* @param localDate
*/
public static Date localDateToDate(LocalDate date) {
ZonedDateTime zonedDateTime = date.atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
}
/**
* LocalDateTime转Date
* @param time
*/
public static Date localDateTimeToDate(LocalDateTime dateTime) {
ZonedDateTime zdt = dateTime.atZone(ZoneId.systemDefault());
return Date.from(zdt.toInstant());
}
/**
* Date转LocalDate
* @param date
*/
public static LocalDate dateToLocalDate(Date date) {
Instant instant = date.toInstant();
LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
return localDate;
}
/**
* Date转LocalDateTime
* @param date
*/
public static LocalDateTime dateToLocalDateTime(Date date) {
Instant instant = date.toInstant();
LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
return localDateTime;
}
/**
* 返回格式化后的日期, 格式为yyyy-MM-dd
* @return
*/
public static final String formatLocalDate(LocalDate date) {
return date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
/**
* 解析字符串, 格式为yyyy-MM-dd
* @return
*/
public static final LocalDate parseToLocalDate (String yyyyMMdd){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return LocalDate.parse(yyyyMMdd, dateTimeFormatter);
}
public static LocalDate addDay(LocalDate localDate, long days){
return ChronoUnit.DAYS.addTo(localDate, days);
}
public static LocalDate addHour(LocalDate localDate, long hour){
return ChronoUnit.HOURS.addTo(localDate, hour);
}
public static long betweenDay(LocalDate start, LocalDate end){
return ChronoUnit.DAYS.between(start, end);
}
public static long betweenHour(LocalDateTime start, LocalDateTime end){
return ChronoUnit.HOURS.between(start, end);
}
public static long betweenSeconds(LocalDateTime start, LocalDateTime end){
return ChronoUnit.SECONDS.between(start, end);
}
JAVA 8 时间处理类
于 2019-08-06 15:19:37 首次发布