编写时间类的工具类

编写时间类的工具类

文章目录

  • 编写时间类的工具类
    • 时间工具类DateTimeUtil

时间工具类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);
        }
    }
}


你可能感兴趣的:(基础稳固,开发语言,java)