时间戳转0点0分0秒(truncatedTo 当天当月当年)

public static void main(String[] args) {
        /*
         * truncatedTo方法介绍:
         * 返回LocalDateTime的副本,其中时间被截断。
         *
         * 截断返回原始日期-时间的副本,其中小于指定单元的字段设置为零。
         * 例如,使用minutes单元进行截断将把秒分和纳秒字段设置为零。
         *
         * 单位的持续时间必须是一个标准日的长度,没有余数。
         * 这包括ChronoUnit和DAYS上提供的所有时间单位。
         * 其他单元抛出异常。
         */
        //时间戳
        long ts = 1615797812000L; // 2021-03-15 16:43:32
        //时间戳转LocalDateTime
        LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneOffset.ofHours(8));
        //当时分0秒
        long l = time.truncatedTo(ChronoUnit.MINUTES).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        System.out.println(l); // 1615797780000 2021-03-15 16:43:00
        //当时0分0秒
        l = time.truncatedTo(ChronoUnit.HOURS).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        System.out.println(l); // 1615795200000 2021-03-15 16:00:00
        //当天0点
        l = time.truncatedTo(ChronoUnit.DAYS).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        System.out.println(l); // 1615737600000 2021-03-15 00:00:00
        //当月0点
        l = time.truncatedTo(ChronoUnit.DAYS).withDayOfMonth(1).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        System.out.println(l); // 1614528000000 2021-03-01 00:00:00
        //当年0点
        l = time.truncatedTo(ChronoUnit.DAYS).withDayOfYear(1).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        System.out.println(l); // 1609430400000 2021-01-01 00:00:00
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

fool_dawei

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

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

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

打赏作者

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

抵扣说明:

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

余额充值