Java8时间工具类

package com.abi.utils;

import lombok.experimental.UtilityClass;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

@UtilityClass
public class DateTimeUtils {

    public final String PATTERN_1 = "yyyy-MM-dd HH:mm:ss";
    public final String PATTERN_2 = "yyyy-MM-dd";
    public final String PATTERN_3 = "yyyy-MM";

    /**
     * 得到当前月的第一天(yyyy-MM-dd HH:mm:ss)
     */
    public LocalDateTime getFirstDayOfMonth() {
        return getFirstDayOfMonth(LocalDateTime.now());
    }


    /**
     * 得到指定时间所在月的第一天(yyyy-MM-dd HH:mm:ss)
     */
    public LocalDateTime getFirstDayOfMonth(LocalDateTime localDateTime) {
        return getStartTimeWithNano0(localDateTime.with(TemporalAdjusters.firstDayOfMonth()));
    }


    /**
     * 得到当前月的最后一天(yyyy-MM-dd HH:mm:ss)
     */
    public LocalDateTime getLastDayOfMonth() {
        return getLastDayOfMonth(LocalDateTime.now());
    }


    /**
     * 得到指定时间所在月的最后一天(yyyy-MM-dd HH:mm:ss)
     */
    public LocalDateTime getLastDayOfMonth(LocalDateTime localDateTime) {
        return getEndTimeWithNano0(localDateTime.with(TemporalAdjusters.lastDayOfMonth()));
    }


    /**
     * 得到当前月的前一个月的第一天(yyyy-MM-dd HH:mm:ss)
     */
    public LocalDateTime getFirstDayOfLastMonth() {
        return getFirstDayOfLastMonth(LocalDateTime.now());
    }


    /**
     * 得到指定时间所在月的前一个月的第一天(yyyy-MM-dd HH:mm:ss)
     */
    public LocalDateTime getFirstDayOfLastMonth(LocalDateTime localDateTime) {
        return getFirstDayOfMonth(localDateTime.minusMonths(1));
    }


    /**
     * 得到当前月的前一个月的最后一天(yyyy-MM-dd HH:mm:ss)
     */
    public LocalDateTime getLastDayOfLastMonth() {
        return getLastDayOfLastMonth(LocalDateTime.now());
    }


    /**
     * 得到指定时间所在月的前一个月的最后一天(yyyy-MM-dd HH:mm:ss)
     */
    public LocalDateTime getLastDayOfLastMonth(LocalDateTime localDateTime) {
        return getLastDayOfMonth(localDateTime.minusMonths(1));
    }


    /**
     * 设置开始时间,不带毫秒,纳秒
     */
    public LocalDateTime getStartTimeWithNano0(LocalDateTime localDateTime) {
        return localDateTime.withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);
    }


    /**
     * 设置结束时间,不带毫秒,纳秒
     */
    public LocalDateTime getEndTimeWithNano0(LocalDateTime localDateTime) {
        return localDateTime.withHour(23)
                .withMinute(59)
                .withSecond(59)
                .withNano(0);
    }


    /**
     * 格式化
     */
    public String format(LocalDateTime localDateTime, String pattern) {
        if (localDateTime != null) {
            return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
        }
        return "";
    }


    /**
     * convert Date to LocalDateTime
     */
    public LocalDateTime convertToLocalDateTime(Date date) {
        if (date != null) {
            return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        }
        return null;
    }


    /**
     * convert LocalDateTime to Date
     */
    public Date convertToDate(LocalDateTime localDateTime) {
        if (localDateTime != null) {
            return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        }
        return null;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值