java LocalDateTime,LocalDate, LocalTime,Date相互转换

文章介绍了Java工具类LocalDateFormatUtils中的一系列方法,用于处理时间戳、日期字符串和Date对象之间的转换,以及日期时间格式的解析和格式化操作。
摘要由CSDN通过智能技术生成

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());
    }
}

码字不易,于你有利,勿忘点赞

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值