废话不多说,直接上代码

 //时间戳转LocalDateTime
    public static LocalDateTime getLocalDateTime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    //获取当月最后一天
    public static LocalDateTime getLastDayOfCurrentMonth(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.lastDayOfMonth()).toLocalDate().atStartOfDay();
    }

    //获取当月第一天
    public static LocalDateTime getFirstDayOfCurrentMonth(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.firstDayOfMonth()).toLocalDate().atStartOfDay();
    }

    //凌晨
    public static LocalDateTime getMorning(LocalDateTime localDateTime) {
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime.toLocalDate().atStartOfDay(), ZoneId.systemDefault());
        return zonedDateTime.toLocalDateTime();
    }

    //返回月份之间的月份
    public static List<String> getMonthBetween(LocalDateTime localDateTime, int minMonth, int maxMonth) {
        LocalDateTime min = localDateTime.withMonth(minMonth).with(TemporalAdjusters.firstDayOfMonth());
        LocalDateTime max = localDateTime.withMonth(maxMonth).with(TemporalAdjusters.lastDayOfMonth());
        List<String> result = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        while (min.isBefore(max)) {
            result.add(min.format(formatter));
            min = min.plusMonths(1);
        }
        return result;
    }

 

相关文章:

  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2021-04-05
  • 2021-12-15
  • 2021-07-26
  • 2021-09-25
猜你喜欢
  • 2021-04-19
  • 2022-12-23
  • 2022-12-23
  • 2022-02-03
  • 2022-03-07
  • 2021-05-21
  • 2021-09-30
相关资源
相似解决方案