编写时间类的工具类

发布时间:2024年01月19日

编写时间类的工具类

时间工具类DateTimeUtil

package com.kang.redisson.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

/**
 * @Author Emperor Kang
 * @ClassName DateTimeUtil
 * @Description 时间工具
 * @Date 2024/1/19 16:32
 * @Version 1.0
 * @Motto 让营地比你来时更干净
 */
public class DateTimeUtil {
    private static final String DATE_FORMATTER = "yyyy-MM-dd";
    private static final String DATE_TIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";

    /**
     * 将LocalDate转换为LocalDateTime
     * @param localDate
     * @return
     */
    public static LocalDateTime localDateToLocalDateTime(LocalDate localDate) {
        if(Objects.isNull(localDate)){
            return null;
        }
        return localDate.atStartOfDay();
    }

    /**
     * 将LocalDateTime转换为LocalDate
     * @param localDateTime
     * @return
     */
    public static LocalDate localDateTimeToLocalDate(LocalDateTime localDateTime) {
        if(Objects.isNull(localDateTime)){
            return null;
        }
        return localDateTime.toLocalDate();
    }

    /**
     * 将Date转换为LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        if(Objects.isNull(date)){
            return null;
        }
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /**
     * 将LocalDateTime转换为Date
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        if(Objects.isNull(localDateTime)){
            return null;
        }
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 将LocalDate转换为Date
     * @param localDate
     * @return
     */
    public static Date localDateToDate(LocalDate localDate) {
        if(Objects.isNull(localDate)){
            return null;
        }
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 将Date转换为LocalDate
     * @param date
     * @return
     */
    public static LocalDate dateToLocalDate(Date date) {
        if(Objects.isNull(date)){
            return null;
        }
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * 将LocalDate转换为String
     * @param localDate
     * @return
     */
    public static String localDateToString(LocalDate localDate) {
        if(Objects.isNull(localDate)){
            return null;
        }
        return localDate.format(DateTimeFormatter.ofPattern(DATE_FORMATTER));
    }

    /**
     * 将LocalDateTime转换为String
     * @param localDateTime
     * @return
     */
    public static String localDateTimeToString(LocalDateTime localDateTime) {
        if(Objects.isNull(localDateTime)){
            return null;
        }
        return localDateTime.format(DateTimeFormatter.ofPattern(DATE_TIME_FORMATTER));
    }

    /**
     * 将Date转换为String
     * @param date
     * @return
     */
    public static String dateToString(Date date) {
        if(Objects.isNull(date)){
            return null;
        }
        return new SimpleDateFormat(DATE_TIME_FORMATTER).format(date);
    }

    /**
     * 将String转换为LocalDate
     * @param dateString
     * @return
     */
    public static LocalDate stringToLocalDate(String dateString) {
        if(StringUtils.isBlank(dateString)){
            return null;
        }
        return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(DATE_FORMATTER));
    }

    /**
     * 将String转换为LocalDateTime
     * @param dateTimeString
     * @return
     */
    public static LocalDateTime stringToLocalDateTime(String dateTimeString) {
        if(StringUtils.isBlank(dateTimeString)){
            return null;
        }
        return LocalDateTime.parse(dateTimeString, DateTimeFormatter.ofPattern(DATE_TIME_FORMATTER));
    }

    /**
     * 将String转换为Date
     * @param dateString
     * @return
     */
    public static Date stringToDate(String dateString) {
        if(StringUtils.isBlank(dateString)){
            return null;
        }
        try {
            return new SimpleDateFormat(DATE_TIME_FORMATTER).parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}


文章来源:https://blog.csdn.net/LuckFairyLuckBaby/article/details/135703102
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。