使用jodatime得到所有小时,所有日期与所有月份集合

    public List<Long> getHoursTimeOfDate(DateTime date) {
        final ImmutableList.Builder<Long> hourTimeList = ImmutableList.builder();

        DateTime firstHourTime = date.withTimeAtStartOfDay();
        final DateTime nextDayFirstHourTime = firstHourTime.plusDays(1);
        while (firstHourTime.isBefore(nextDayFirstHourTime)) {
            hourTimeList.add(firstHourTime.getMillis());
            firstHourTime = firstHourTime.plusHours(1);
        }
        return hourTimeList.build();

    }

    public List<Long> getDaysOfMonth(DateTime date) {
        final ImmutableList.Builder<Long> dayList = ImmutableList.builder();

        LocalDate firstDay = date.toLocalDate().withDayOfMonth(1);
        final LocalDate nextMonthFirstDay = firstDay.plusMonths(1);
        while (firstDay.isBefore(nextMonthFirstDay)) {
            dayList.add(firstDay.toDateTimeAtStartOfDay().getMillis());
            firstDay = firstDay.plusDays(1);
        }
        return dayList.build();
    }

    public List<Long> getMonthsOfYear(DateTime date) {
        final ImmutableList.Builder<Long> monthList = ImmutableList.builder();

        LocalDate firstMonth = date.toLocalDate().withDayOfMonth(1).withMonthOfYear(1);
        final LocalDate nextYearFirstMonth = firstMonth.plusYears(1);
        while (firstMonth.isBefore(nextYearFirstMonth)) {
            monthList.add(firstMonth.toDateTimeAtStartOfDay().getMillis());
            firstMonth = firstMonth.plusMonths(1);
        }
        return monthList.build();
    }

猜你喜欢

转载自sunxboy.iteye.com/blog/1992922