import org.apache.commons.lang3.StringUtils;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;
public class LocalDateFormatUtils {
public static final String YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
public static final String YYYY_MM_DD_HH_MI_SS_SSS = "yyyy-MM-dd HH:mm:ss:SSS";
public static final String YYYY_MM_DD = "yyyy-MM-dd";
public static final String YYYYMMDD = "yyyyMMdd";
public static final String YYYYMMDDHHMISS = "yyyyMMddHHmmss";
public static final String YYYYMMDDHHMISSSSS = "yyyyMMddHHmmssSSS";
public static final String HHMISS = "HHmmss";
public static final String HHMISSSSS = "HHmmssSSS";
public static final String HH_MI_SS = "HH:mm:ss";
public static final String HH_MI_SS_SSSS = "HH:mm:ss:SSS";
/**
* 时间戳转LocalDateTime
* @param timeMillis
* @return LocalDateTime
*/
public static LocalDateTime parseLocalDateTime(Long timeMillis) {
if (Objects.isNull(timeMillis)) {
return null;
}
return LocalDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), ZoneOffset.systemDefault());
}
/**
* 时间字符串 转 LocalDateTime
* @param text
* @return LocalDateTime
*/
public static LocalDateTime parseLocalDateTime(String text) {
if (StringUtils.isBlank(text)) {
return null;
}
return LocalDateTime.parse(text, DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MI_SS));
}
/**
* 时间字符串 转 LocalDateTime
* @param text
* @param format
* @return LocalDateTime
*/
public static LocalDateTime parseLocalDateTime(String text, String format) {
if (StringUtils.isBlank(text)) {
return null;
}
return LocalDateTime.parse(text, DateTimeFormatter.ofPattern(Optional.ofNullable(format).orElse(YYYY_MM_DD_HH_MI_SS)));
}
/**
* Date 转 LocalDateTime
* @param date
* @return LocalDateTime
*/
public static LocalDateTime parseLocalDateTime(Date date) {
if (Objects.isNull(date)) {
return null;
}
return LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.systemDefault());
}
/**
* 时间戳 转 LocalDate
* @param timeMillis
* @return LocalDate
*/
public static LocalDate parseLocalDate(Long timeMillis) {
if (Objects.isNull(timeMillis)) {
return null;
}
return LocalDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), ZoneOffset.systemDefault()).toLocalDate();
}
/**
* 日期字符串 转 LocalDate
* @param text
* @return LocalDate
*/
public static LocalDate parseLocalDate(String text) {
if (StringUtils.isBlank(text)) {
return null;
}
return LocalDate.parse(text, DateTimeFormatter.ofPattern(YYYY_MM_DD));
}
/**
* 日期字符串 转 LocalDate
* @param text
* @param format
* @return LocalDate
*/
public static LocalDate parseLocalDate(String text, String format) {
if (StringUtils.isBlank(text)) {
return null;
}
return LocalDate.parse(text, DateTimeFormatter.ofPattern(Optional.ofNullable(format).orElse(YYYY_MM_DD)));
}
/**
* Date 转 LocalDate
* @param date
* @return LocalDate
*/
public static LocalDate parseLocalDate(Date date) {
if (Objects.isNull(date)) {
return null;
}
return LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.systemDefault()).toLocalDate();
}
/**
* 时间戳 转 LocalTime
* @param timeMillis
* @return LocalTime
*/
public static LocalTime parseLocalTime(Long timeMillis) {
if (Objects.isNull(timeMillis)) {
return null;
}
return LocalDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), ZoneOffset.systemDefault()).toLocalTime();
}
/**
* 时分秒字符串 转 LocalTime
* @param text
* @return
*/
public static LocalTime parseLocalTime(String text) {
if (StringUtils.isBlank(text)) {
return null;
}
return LocalTime.parse(text, DateTimeFormatter.ofPattern(HH_MI_SS));
}
/**
* 时分秒字符串 转 LocalTime
* @param text
* @param format
* @return LocalTime
*/
public static LocalTime parseLocalTime(String text, String format) {
if (StringUtils.isBlank(text)) {
return null;
}
return LocalTime.parse(text, DateTimeFormatter.ofPattern(Optional.ofNullable(format).orElse(HH_MI_SS)));
}
/**
* Date 转 LocalTime
* @param date
* @return LocalTime
*/
public static LocalTime parseLocalTime(Date date) {
if (Objects.isNull(date)) {
return null;
}
return LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.systemDefault()).toLocalTime();
}
/**
* localDateTime 转成 Date
* @param localDateTime
* @return Date
*/
public static Date parseDate(LocalDateTime localDateTime) {
if (Objects.isNull(localDateTime)) {
return null;
}
return Date.from(localDateTime.atZone(ZoneOffset.systemDefault()).toInstant());
}
/**
* localDate 转成 Date,时分秒取 00:00:00:
* @param localDate
* @return Date
*/
public static Date parseDate(LocalDate localDate) {
if (Objects.isNull(localDate)) {
return null;
}
return Date.from(localDate.atStartOfDay().atZone(ZoneOffset.systemDefault()).toInstant());
}
/**
* localTime 转成 Date,日期取当前日期
* @param localTime
* @return Date
*/
public static Date parseDate(LocalTime localTime) {
if (Objects.isNull(localTime)) {
return null;
}
return Date.from(localTime.atDate(LocalDate.now()).atZone(ZoneOffset.systemDefault()).toInstant());
}
/**
* localDateTime字符格式化
* @param localDateTime
* @return String
*/
public static String format(LocalDateTime localDateTime) {
if (Objects.isNull(localDateTime)) {
return null;
}
return localDateTime.format(DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MI_SS));
}
/**
* localDateTime字符格式化
* @param localDateTime
* @param formatter
* @return String
*/
public static String format(LocalDateTime localDateTime, String formatter) {
if (Objects.isNull(localDateTime)) {
return null;
}
return localDateTime.format(DateTimeFormatter.ofPattern(Optional.ofNullable(formatter).orElse(YYYY_MM_DD_HH_MI_SS)));
}
/**
* localDate字符格式化
* @param localDate
* @return String
*/
public static String format(LocalDate localDate) {
if (Objects.isNull(localDate)) {
return null;
}
return localDate.format(DateTimeFormatter.ofPattern(YYYY_MM_DD));
}
/**
* localDate字符格式化
* @param localDate
* @param formatter
* @return String
*/
public static String format(LocalDate localDate, String formatter) {
if (Objects.isNull(localDate)) {
return null;
}
return localDate.format(DateTimeFormatter.ofPattern(Optional.ofNullable(formatter).orElse(YYYY_MM_DD)));
}
/**
* localTime字符格式化
* @param localTime
* @return String
*/
public static String format(LocalTime localTime) {
if (Objects.isNull(localTime)) {
return null;
}
return localTime.format(DateTimeFormatter.ofPattern(HH_MI_SS));
}
/**
* localTime字符格式化
* @param localTime
* @param formatter
* @return String
*/
public static String format(LocalTime localTime, String formatter) {
if (Objects.isNull(localTime)) {
return null;
}
return localTime.format(DateTimeFormatter.ofPattern(Optional.ofNullable(formatter).orElse(HH_MI_SS)));
}
/**
* 获得时间戳
* @param localDateTime
* @return LocalDateTime
*/
public static Long toEpochMilli(LocalDateTime localDateTime) {
if (Objects.isNull(localDateTime)) {
return null;
}
return localDateTime.atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli();
}
/**
* 获得时间戳
* @param localDate
* @return Long
*/
public static Long toEpochMilli(LocalDate localDate) {
if (Objects.isNull(localDate)) {
return null;
}
return localDate.atStartOfDay().atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli();
}
/**
* 获取日期最小时间
* @param localDateTime
* @return LocalDateTime
*/
public static LocalDateTime getBeginOfDay(LocalDateTime localDateTime) {
if (Objects.isNull(localDateTime)) {
return null;
}
return LocalDateTime.of(localDateTime.toLocalDate(), localDateTime.toLocalTime().with(LocalTime.MIN));
}
/**
* 获取日期最大时间
* @param localDateTime
* @return LocalDateTime
*/
public static LocalDateTime getEndOfDay(LocalDateTime localDateTime) {
if (Objects.isNull(localDateTime)) {
return null;
}
return LocalDateTime.of(localDateTime.toLocalDate(), localDateTime.toLocalTime().with(LocalTime.MAX));
}
/**
* 获取日期最小时间
* @param date
* @return LocalDateTime
*/
public static LocalDateTime getBeginOfDay(Date date) {
if (Objects.isNull(date)) {
return null;
}
ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneOffset.systemDefault());
return LocalDateTime.of(zonedDateTime.toLocalDate(), zonedDateTime.toLocalTime().with(LocalTime.MIN));
}
/**
* 获取日期最大时间
* @param date
* @return LocalDateTime
*/
public static LocalDateTime getEndOfDay(Date date) {
if (Objects.isNull(date)) {
return null;
}
ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneOffset.systemDefault());
return LocalDateTime.of(zonedDateTime.toLocalDate(), zonedDateTime.toLocalTime().with(LocalTime.MAX));
}
/**
* 获取日期最小时间
* @param localDateTime
* @return Date
*/
public static Date getBeginOfDayToDate(LocalDateTime localDateTime) {
if (Objects.isNull(localDateTime)) {
return null;
}
LocalDateTime newLocalDateTime = getBeginOfDay(localDateTime);
return parseDate(newLocalDateTime);
}
/**
* 获取日期最大时间
* @param localDateTime
* @return Date
*/
public static Date getEndOfDayToDate(LocalDateTime localDateTime) {
if (Objects.isNull(localDateTime)) {
return null;
}
LocalDateTime newLocalDateTime = getEndOfDay(localDateTime);
return parseDate(newLocalDateTime);
}
/**
* 获取日期最小时间
* @param date
* @return Date
*/
public static Date getBeginOfDayToDate(Date date) {
if (Objects.isNull(date)) {
return null;
}
LocalDateTime localDateTime = getBeginOfDay(date);
return parseDate(localDateTime);
}
/**
* 获取日期最大时间
* @param date
* @return Date
*/
public static Date getEndOfDayToDate(Date date) {
if (Objects.isNull(date)) {
return null;
}
LocalDateTime localDateTime = getEndOfDay(date);
return parseDate(localDateTime);
}
/**
* 获取当月月初(第一天)
* */
public static LocalDate getMonthFirst() {
return LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
}
/**
* 获取当月月末(最后一天)
* */
public static LocalDate getMonthLast() {
return LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
}
/**
* 获取当月月初(第一天)
* */
public static LocalDate getMonthFirst(LocalDate localDate) {
if (Objects.isNull(localDate)) {
return null;
}
return localDate.with(TemporalAdjusters.firstDayOfMonth());
}
/**
* 获取当月月末(最后一天)
* */
public static LocalDate getMonthLast(LocalDate localDate) {
if (Objects.isNull(localDate)) {
return null;
}
return localDate.with(TemporalAdjusters.lastDayOfMonth());
}
}
码字不易,于你有利,勿忘点赞