关于时间日期工具类

package com.packet.utils;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 日期时间工具类
 */
@Slf4j
public class DateUtil {

    private static final String patternDateTime = "yyyy-MM-dd HH:mm:ss";
    public static final String patternDateTimeMillisecond = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String patternDateTimeOfMonth = "yyyy-MM-dd";

    private static final String EXPORT_FORMAT_DATE_TIME = "yyyyMMdd-hhmmss";
    public static List<Integer> holidays = Lists.newArrayList(6, 7);
    public static List<Integer> workdays = Lists.newArrayList(1, 2, 3, 4, 5);

    /**
     * java8 LocalDateTime转String
     *
     * @param dateTime
     * @return
     */
    public static String formatDateTime(LocalDateTime dateTime) {
        if (Objects.isNull(dateTime)) {
            return "";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(patternDateTime);
        String dateTimeStr = dateTime.format(formatter);
        return dateTimeStr;
    }
    public static String formatDateTimeOfMonth(LocalDateTime dateTime) {
        if (Objects.isNull(dateTime)) {
            return "";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(patternDateTimeOfMonth);
        String dateTimeStr = dateTime.format(formatter);
        return dateTimeStr;
    }

    public static String dateTimestamp(LocalDateTime date) {
        return DateTimeFormatter.ofPattern("yyMMddHHmmssSSS").format(date);
    }

    public static String formatDateTime(LocalDateTime dateTime, String pattern) {
        if (Objects.isNull(dateTime)) {
            return "";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        String dateTimeStr = dateTime.format(formatter);
        return dateTimeStr;
    }

    public static String formatDateTime(Date dateTime) {
        if (Objects.nonNull(dateTime)) {
            return DateFormatUtils.format(dateTime, patternDateTimeOfMonth);
        }
        return "";
    }

    public static String formatDateOfDate(Date dateTime) {
        if (Objects.nonNull(dateTime)) {
            return DateFormatUtils.format(dateTime, patternDateTime);
        }
        return "";
    }

    /**
     * java8 LocalDateTime转String
     *
     * @param dateTime
     * @return
     */
    public static String exportFormatDateTime(Date dateTime) {
        if (Objects.nonNull(dateTime)) {
            return DateFormatUtils.format(dateTime, EXPORT_FORMAT_DATE_TIME);
        }
        return "";
    }

    public static String formatDateTimeSimple(Date dateTime) {
        if (Objects.nonNull(dateTime)) {
            return DateFormatUtils.format(dateTime, "yyyy-MM-dd");
        }
        return "";
    }

    public static String formatDate(Date dateTime) {
        if (Objects.nonNull(dateTime)) {
            return DateFormatUtils.format(dateTime, "yyyy.MM.dd");
        }
        return "";
    }

    public static String formatDateTime(Date dateTime, String pattern) {
        if (Objects.nonNull(dateTime)) {
            return DateFormatUtils.format(dateTime, pattern);
        }
        return "";
    }

    public static Date strToDate(String string) throws ParseException {
        Date date = null;
        date = DateUtils.parseDate(string, "yyyy-MM-dd");
        return date;
    }

    public static LocalDateTime strToLocalDateTime(String string) throws ParseException {
        Date date = null;
        date = DateUtils.parseDate(string, "yyyy-MM-dd HH:mm:ss");
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
    public static LocalDateTime strToLocalDateTimeMillisecond(String string) throws ParseException {
        Date date = null;
        date = DateUtils.parseDate(string, patternDateTimeMillisecond);
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static Date strToDate3(String string) throws ParseException {
        Date date = null;
        date = DateUtils.parseDate(string, patternDateTime);
        return date;
    }

    /**
     * 传入的开始时间和现在的时间差Duration
     *
     * @param beginDate
     * @return
     */
    public static Duration timeDifferenceToDuration(Date beginDate) {
        return Duration
            .between(beginDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(), LocalDateTime.now());
    }

    /**
     * 传入的开始时间和结束时间时间差Duration
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static Duration timeDifferenceToDuration(Date beginDate, Date endDate) {
        return Duration.between(beginDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(),
            endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
    }

    /**
     * 获得某天最大时间 2019-10-15 23:59:59
     */
    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime =
            LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        ;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获得某天最小时间 2019-10-15 00:00:00
     */
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime =
            LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔(天)
     *
     * @param date1 开始时间
     * @param date2 截止时间
     * @return 返回天数
     */
    public static int differenceBetween(Date date1, Date date2) {
        int days = (int)(((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
        return days;
    }

    public static void main(String[] args) {
        // String begin = "2020-09-11 11:00:00";
        // String end = "2020-09-19 17:57:00";
        // Duration duration = timeDifferenceToDuration(DateUtil.strToDate3(begin), DateUtil.strToDate3(end));
        // System.out.println(duration.toDays());
        System.out.println(exportFormatDateTime(new Date()));
        LocalDateTime removeLogTime = LocalDateTime.now().plusDays(-100);
        System.out.println(removeLogTime);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java小白笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值