日期时间工具类

package com.xxx.xxx.common.utils;

import com.xxx.xxx.common.enums.ResultCodeEnum;
import com.xxx.xxx.common.exception.InterceptorException;
import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 * @description: 日期时间工具类
 * @author: Shinka
 * @date: 2021-08-24
 */
public final class DateUtil {

    private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static DateTimeFormatter dateTimeFormatterDeatil = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    private static String rex = "\\d{4}-\\d{2}-\\d{2}";


    /**
     * Date 转 ZonedDateTime
     * @param date
     * @return
     */
    public static ZonedDateTime date2ZoneDateTime(Date date){
        return date == null ? null : ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }


    /**
     * String(yyyy-MM-dd) 转 Date
     * @param StringDate
     * @return
     */
    public static Date string2Date(String StringDate) {
        if(StringUtils.isEmpty(StringDate)){
            throw new InterceptorException(ResultCodeEnum.STRING_IS_NULL.code(), ResultCodeEnum.STRING_IS_NULL.message());
        }

        if(!StringDate.matches(rex)){
            throw new InterceptorException(ResultCodeEnum.DATE_FORMAT_NOT_TRUE.code(), ResultCodeEnum.DATE_FORMAT_NOT_TRUE.message());
        }
        Date date = null;
        try {
            date = simpleDateFormat.parse(StringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * String(yyyy-MM-dd) 转 ZonedDateTime
     *
     * @param StringDate 字符串日期
     * @return ZonedDateTime
     */
    public static ZonedDateTime string2ZonedDateTime(String StringDate) {
        if (StringUtils.isEmpty(StringDate)) {
            return null;
        }

        if (!StringDate.matches(rex)) {
            throw new InterceptorException(ResultCodeEnum.DATE_FORMAT_NOT_TRUE.code(), ResultCodeEnum.DATE_FORMAT_NOT_TRUE.message());
        }
        ZonedDateTime zonedDateTime = null;
        try {
            zonedDateTime = LocalDate.parse(StringDate, dateTimeFormatter).atStartOfDay(ZoneId.systemDefault());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zonedDateTime;
    }

    /**
     * string(yyyy-MM-dd) 转 ZonedDateTime+1天,主要用于日期至
     * @param date yyyy-mm-dd
     * @return zonedDateTime
     */
    public static ZonedDateTime string2ZonedDateTimeAddOne(String date){
        if (StringUtils.isEmpty(date)) {
            return null;
        }

        Calendar c = Calendar.getInstance();
        c.setTime(DateUtil.string2Date(date));
        c.add(Calendar.DAY_OF_MONTH, 1);
        Date time = c.getTime();
        ZonedDateTime dateTo = DateUtil.date2ZoneDateTime(time);
        return dateTo;
    }

    /**
     * String(yyyy-MM-dd) 转 ZonedDateTime结束时间(系统时区)
     * @param date
     * @return
     */
    public static ZonedDateTime getDayEndDate(String date) {
        return LocalDateTime.of(LocalDate.parse(date, dateTimeFormatter), LocalTime.MAX)
                .atZone(ZoneId.systemDefault());
    }


    /**
     * ZonedDateTime 转 String(yyyy-MM-dd)
     * @param dateTime
     * @return
     */
    public static String zonedDateTimeToDateString(ZonedDateTime dateTime) {
        return zonedDateTimeToString(dateTime, dateTimeFormatter);
    }

    /**
     * ZonedDateTime 转 String(yyyy-MM-dd HH:mm:ss)
     * @param dateTime
     * @return
     */
    public static String zonedDateTimeToDateTimeString(ZonedDateTime dateTime) {
        return zonedDateTimeToString(dateTime, dateTimeFormatterDeatil);
    }

    /**
     * ZonedDateTime 转 String(指定格式)
     * @param dateTime
     * @param formatter
     * @return
     */
    public static String zonedDateTimeToString(ZonedDateTime dateTime, DateTimeFormatter formatter) {
        if (dateTime == null) {
            return null;
        }
        String str = null;
        try {
            str = dateTime.format(formatter);
        } catch (Exception e) {
            e.printStackTrace();
            return str;
        }
        return str;
    }


    /**
     * 今天的开始时间(系统时区)
     * @return
     */
    public static ZonedDateTime getCurrentDayStartDate() {
        return LocalDate.now().atStartOfDay(ZoneId.systemDefault());
    }

    /**
     * 所传入ZonedDateTime的开始时间(默认为系统时区)
     * @param zonedDateTime
     * @return
     */
    public static ZonedDateTime truncDate(ZonedDateTime zonedDateTime) {
        return null == zonedDateTime ? null : zonedDateTime.truncatedTo(ChronoUnit.DAYS);
    }

    /**
     * 所传入ZonedDateTime的开始时间(UTC时间)
     * @param zonedDateTime
     * @return
     */
    public static ZonedDateTime getStartDateByUTC(ZonedDateTime zonedDateTime) {
        return null == zonedDateTime ? null : zonedDateTime.toLocalDate().atStartOfDay(ZoneId.of("UTC"));
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值