java8时间工具类
package com.test.test.utils;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
public class LocalDateTimeUtils {
public static final String DATE_FORMAT_NORMAL = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_NUMBER = "yyyyMMddHHmmss";
public static final String DATE_FORMAT_T = "yyyy-MM-ddTHH:mm:ss";
public static Date localDateTime2Date(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
public static LocalDateTime date2LocalDateTime(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
public static String localDateTime2String(LocalDateTime localDateTime) {
return localDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT_NORMAL));
}
public static String localDateTime2String(LocalDateTime localDateTime, String datePattern) {
return localDateTime.format(DateTimeFormatter.ofPattern(datePattern));
}
public static LocalDateTime string2LocalDateTime(String dateString, String datePattern) {
return LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(datePattern));
}
public static long localDateTime2Timestamp(LocalDateTime localDateTime) {
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
}
public static LocalDateTime timestamp2LocalDateTime(long timestamp) {
return LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
}
public static long localDateTime2TimeMillis(LocalDateTime localDateTime) {
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
public static LocalDateTime timeMillis2LocalDateTime(long timeMillis) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), ZoneId.systemDefault());
}
public static LocalDateTime getYearStart(LocalDateTime localDateTime) {
return localDateTime.with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN);
}
public static LocalDateTime getYearStart(int offset) {
return LocalDateTime.now().plusYears(offset).with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN);
}
public static LocalDateTime getYearEnd(LocalDateTime localDateTime) {
return localDateTime.with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX);
}
public static LocalDateTime getYearEnd(int offset) {
return LocalDateTime.now().plusYears(offset).with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX);
}
public static LocalDateTime getMonthStart(LocalDateTime localDateTime) {
return localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
}
public static LocalDateTime getMonthStart(int offset) {
return LocalDateTime.now().plusMonths(offset).with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
}
public static LocalDateTime getMonthEnd(LocalDateTime localDateTime) {
return localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
}
public static LocalDateTime getMonthEnd(int offset) {
return LocalDateTime.now().plusMonths(offset).with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
}
public static LocalDateTime getDayStart(LocalDateTime localDateTime) {
return localDateTime.with(LocalTime.MIN);
}
public static LocalDateTime getDayStart(int offset) {
return LocalDateTime.now().plusDays(offset).with(LocalTime.MIN);
}
public static LocalDateTime getDayEnd(LocalDateTime localDateTime) {
return localDateTime.with(LocalTime.MAX);
}
public static LocalDateTime getDayEnd(int offset) {
return LocalDateTime.now().plusDays(offset).with(LocalTime.MAX);
}
}