工具类

java8 时间常见用法工具类

2021-05-24  本文已影响0人  无我_无他_有你

java8 时间常见用法工具类

package pers.darcy.flower.utils.util;

import cn.hutool.core.lang.Assert;

import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.concurrent.TimeUnit;

/**
 * @Author: wqf
 * @Date: 2022/01/14
 * @Description: java8日期相关工具类
 */
public class TimeUtil {
    /**
     * 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss
     */
    public static String timeToString(Long time, String format) {
        Assert.notNull(time, "time is null");
        // "yyyy-MM-dd HH:mm:ss"
        DateTimeFormatter formatString = DateTimeFormatter.ofPattern(format);
        return formatString.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
    }

    /**
     * 将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss
     */
    public static Long timeToLong(String time) {
        Assert.notNull(time, "time is null");
        DateTimeFormatter formatString = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(time, formatString);
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 获取本周一
     */
    public static LocalDateTime firstDayOfWeek(Long date) {
        // long转LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
        return localDateTime.with(DayOfWeek.MONDAY);
    }

    /**
     * 获取上周一
     */
    public static LocalDateTime firstDayOfLastWeek(Long date) {
        // long转LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
        LocalDateTime localDateTime1 = localDateTime.plusDays(-7);
        return localDateTime1.with(DayOfWeek.MONDAY);
    }

    /**
     * 获取上周一
     */
    public static LocalDateTime lastDay(Long date) {
        // long转LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(date), ZoneId.systemDefault());
        return localDateTime.plusDays(-1);
    }


    /**
     * LocalDate转时间戳
     */
    public static Long localDate2Second(LocalDate localDate) {
        return LocalDateTime.of(localDate, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
    }

    /**
     * LocalDate转时间戳
     */
    public static Long localDateTime2Second(LocalDateTime localDateTime) {
        return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
    }


    /**
     * 方法描述: 获取当前时间所在月开始时刻或结束时刻
     *
     * @param localDate   时间
     * @param isStartTime true 返回月开始时刻 false 返回月结束时刻
     * @return void
     * @author wqf
     * @date 2022/1/14 17:35
     */
    public static LocalDate startOrEndOfMonth(LocalDate localDate, boolean isStartTime) {
        if (isStartTime) {
            //本月第一天日期
            return localDate.with(TemporalAdjusters.firstDayOfMonth());
        } else {
            //本月最后一天日期
            return localDate.with(TemporalAdjusters.lastDayOfMonth());
        }
    }

    /**
     * 方法描述: 获取当前时间所在月开始时刻或结束时刻
     *
     * @param localDateTime 时间
     * @return java.time.LocalDate
     * @author wqf
     * @date 2022/1/14 17:38
     */
    public static LocalDateTime startOrEndOfMonth(LocalDateTime localDateTime, boolean isStartTime) {
        if (isStartTime) {
            // 获取当前月第一天
            return LocalDateTime.of(LocalDate.from(localDateTime.with(TemporalAdjusters.firstDayOfMonth())), LocalTime.MIN);
        } else {
            return LocalDateTime.of(LocalDate.from(localDateTime.with(TemporalAdjusters.lastDayOfMonth())), LocalTime.MAX);
        }
    }


    /**
     * 方法描述: 获取当前时间所在天开始时刻或结束时刻
     *
     * @param localDateTime 时间
     * @return java.time.LocalDate
     * @author wqf
     * @date 2022/1/14 17:38
     */
    public static LocalDateTime startOrEndOfDay(LocalDateTime localDateTime, boolean isStartTime) {
        if (isStartTime) {
            return LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        } else {
            return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        }
    }

    /**
     * 方法描述: 获取当前时间所在年开始时刻或结束时刻
     *
     * @param localDateTime 时间
     * @return java.time.LocalDate
     * @author wqf
     * @date 2022/1/14 17:38
     */
    public static LocalDateTime startOrEndOfYear(LocalDateTime localDateTime, boolean isStartTime) {
        if (isStartTime) {
            return LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfYear())), LocalTime.MIN);
        } else {
            return LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.lastDayOfYear())), LocalTime.MAX);
        }
    }

    /**
     * 方法描述: 计算两个时间间隔天数
     *
     * @param fromTime 开始时间
     * @param toTime   结束时间
     * @param timeUnit 时间格式
     * @return long
     * @author wqf
     * @date 2022/1/14 17:56
     */
    public static long numberOfDaysBetweenTwoTimes(LocalDate fromTime, LocalDate toTime, TimeUnit timeUnit) {
        //计算相差多少秒
        if (timeUnit.equals(TimeUnit.SECONDS)) {
            return ChronoUnit.SECONDS.between(fromTime, toTime);
        } else if (timeUnit.equals(TimeUnit.MINUTES)) {
            return ChronoUnit.MINUTES.between(fromTime, toTime);
        } else if (timeUnit.equals(TimeUnit.HOURS)) {
            return ChronoUnit.HOURS.between(fromTime, toTime);
        } else if (timeUnit.equals(TimeUnit.DAYS)) {
            return ChronoUnit.DAYS.between(fromTime, toTime);
        } else {
            throw new IllegalArgumentException("时间格式错误");
        }
    }

    /**
     * 方法描述: 指定日期所在月份的总天数
     *
     * @param localDate 日期
     * @return int
     * @author wqf
     * @date 2022/1/17 13:38
     */
    public static int totalDaysOfMonth(LocalDate localDate) {
        return localDate.withMonth(localDate.getMonthValue()).lengthOfMonth();
    }

    /**
     * 方法描述: 指定日期所在月份的总天数
     *
     * @param localDateTime 日期
     * @return int
     * @author wqf
     * @date 2022/1/17 13:38
     */
    public static int totalDaysOfMonth(LocalDateTime localDateTime) {
        LocalDate localDate = localDateTime.toLocalDate();
        return localDate.withMonth(localDate.getMonthValue()).lengthOfMonth();
    }
}
上一篇下一篇

猜你喜欢

热点阅读