使用Java8新的时间api重构时间工具类

使用Java8新的时间api重构时间工具类

      
package com.chehejia.osd.server.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * Java8日期工具类
 *
 */
public class LocalDateUtils {
    /**
     * 获取当前时间戳
     *
     * @return Integer
     */
    public static Integer currentTs() {
        return (int) LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8));
    }

    /**
     * 获取当前时间
     *
     * @param format String 目标格式,eg: yyyy-MM-dd HH:mm:ss
     * @return String 符合目标格式的时间字符串
     */
    public static String currentDate(String format) {
        return formatTs(currentTs(), format);
    }

    /**
     * 格式化时间戳
     *
     * @param ts     Integer 带格式化的时间戳
     * @param format String 目标格式,eg: yyyy-MM-dd HH:mm:ss
     * @return String 符合目标格式的时间字符串
     */
    public static String formatTs(Integer ts, String format) {
        return toLocalDateTime(ts).format(DateTimeFormatter.ofPattern(format));
    }

    /**
     * 将时间戮转成日期时间
     *
     * @param ts 时间戮
     * @return LocalDateTime
     */
    public static LocalDateTime toLocalDateTime(Integer ts) {
        return LocalDateTime.ofEpochSecond(ts, 0, ZoneOffset.ofHours(8));
    }

    /**
     * 日期时间转时间戮
     *
     * @param localDateTime 日期时间
     * @return Integer
     */
    public static Integer toTs(LocalDateTime localDateTime) {
        return (int) localDateTime.toEpochSecond(ZoneOffset.ofHours(8));
    }

    /**
     * 将指定时间戳转换为对应天的开始时间戳(00:00:00秒)
     *
     * @param ts 时间戳
     * @return Integer
     */
    public static Integer toDayStartTs(Integer ts) {
        LocalDateTime oldLocalDateTime = toLocalDateTime(ts);
        LocalDateTime newLocalDateTime = LocalDateTime.of(oldLocalDateTime.toLocalDate(), LocalTime.MIN);
        return toTs(newLocalDateTime);
    }

    /**
     * 将指定时间戳转换为对应天的结束时间戳(23:59:59秒)
     *
     * @param ts 时间戳
     * @return Integer
     */
    public static Integer toDayEndTs(Integer ts) {
        LocalDateTime oldLocalDateTime = toLocalDateTime(ts);
        LocalDateTime newLocalDateTime = LocalDateTime.of(oldLocalDateTime.toLocalDate(), LocalTime.MAX);
        return toTs(newLocalDateTime);
    }

    /**
     * 将指定时间戳转换为对应上一天的开始时间戳(00:00:00秒)
     *
     * @param ts 时间戳
     * @return Integer
     */
    public static Integer toLastDayStartTs(Integer ts) {
        LocalDateTime oldLocalDateTime = toLocalDateTime(ts);
        LocalDateTime newLocalDateTime = LocalDateTime.of(oldLocalDateTime.toLocalDate().plusDays(-1), LocalTime.MIN);
        return toTs(newLocalDateTime);
    }

    /**
     * 将指定时间戳转换为对应上一天的结束时间戳(23:59:59秒)
     *
     * @param ts 时间戳
     * @return Integer
     */
    public static Integer toLastDayEndTs(Integer ts) {
        LocalDateTime oldLocalDateTime = toLocalDateTime(ts);
        LocalDateTime newLocalDateTime = LocalDateTime.of(oldLocalDateTime.toLocalDate().plusDays(-1), LocalTime.MAX);
        return toTs(newLocalDateTime);
    }

    /**
     * 将时间戳转换为对应月份的开始时间戳
     *
     * @param ts 时间戳
     * @return Integer
     */
    public static Integer toFirstDayOfMonth(Integer ts) {
        LocalDateTime oldLocalDateTime = toLocalDateTime(ts);
        LocalDateTime newLocalDateTime = oldLocalDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
        return toTs(newLocalDateTime);
    }

    /**
     * 将时间戳转换为对应月份的结束时间戳
     *
     * @param: ts   时间戳
     * @return: Integer
     */
    public static Integer toLastDayOfMonth(Integer ts) {
        LocalDateTime oldLocalDateTime = toLocalDateTime(ts);
        LocalDateTime newLocalDateTime = oldLocalDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
        return toTs(newLocalDateTime);
    }

    /**
     * 将String转成指定格式的日期, 再获取时间戳
     *
     * @param: date   日期         例: 2023-01-01
     * @param: format 要转换的格式  例: yyyy-MM-dd
     * @return: Integer
     */
    public static Integer dateToTs(String date, String format) {
        LocalDateTime localDateTime = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(format));
        return toTs(localDateTime);
    }

    /**
     * 获取指定时间戳上周周日(周一至周日算做一周)的结束时间戳(23:59:59秒)
     *
     * @param ts 时间戳
     * @return Integer
     */
    public static Integer toLastSundayEndTs(Integer ts) {
        LocalDateTime oldLocalDateTime = toLocalDateTime(ts);
        LocalDateTime newLocalDateTime = oldLocalDateTime.minusWeeks(1L).with(DayOfWeek.SUNDAY).with(LocalTime.MAX);
        return toTs(newLocalDateTime);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

boy快快长大

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

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

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

打赏作者

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

抵扣说明:

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

余额充值