Java8获取各种需求的时间总汇

    // 获得某天最大时间 
    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());
    }

    // 获得某天最小时间
    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());
    }

     /**
     *  获取传过来的当天时间戳23:59:59的时间
     */
    public static Long getDateEndMs(Long sampleTime) {
        LocalDate todayDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(sampleTime), ZoneId.systemDefault()).toLocalDate();
        LocalDateTime beginTime = LocalDateTime.of(todayDate, LocalTime.MAX);
        return LocalDateTime.from(beginTime).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     *  获取一周以前的零点时刻   2021/3/26 11:28:00 ----> 2021/3/19 00:00:00
     */
    public static Long getRecentWeekUserCount(Long sampleTime) {
        LocalDate today = LocalDateTime.ofInstant(Instant.ofEpochMilli(sampleTime), ZoneId.systemDefault()).toLocalDate();
        LocalDate nextWeek = today.plus(-1, ChronoUnit.WEEKS);
        return nextWeek.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 获取一个月以前那天的零点
     *
     * @return
     */
    public static Long getMonthAgo(){
        LocalDateTime beginTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime date = beginTime.minusMonths(1);
        return LocalDateTime.from(date).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值