Java-计算时间间隔中每月发生的一天

时间:2019-03-01 06:57:30

标签: java

我正在使用Java 8,我需要计算一下索引= x的间隔内有多少天。例如:从1-1-2019到31-1-2019,我们有1天出现在第1天。

我不希望使用天数范围,而只需要X天 如果是一个月的最后一天,我想全部算一下:30 +30 + 31 + 28

5 个答案:

答案 0 :(得分:4)

尝试此方法:

public static int getDateCount(LocalDate startDate, LocalDate endDate, final int index) {
        long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
        return IntStream.iterate(0, i -> i + 1)
                .limit(numOfDaysBetween)
                .mapToObj(i -> startDate.plusDays(i))
                .filter(i -> i.getDayOfMonth() == index)
                .collect(Collectors.toList()).size();
    }

用法:

public static void main(String[] args){
        LocalDate startDate = LocalDate.of(2019,1,1);
        LocalDate endDate = LocalDate.of(2019,1,31);
        int index=1;

        System.out.println(getDateCount(startDate,endDate,index));
    }

输出:

  

1

在这里,我首先计算了两个日期之间的天数,然后提取了这些日期之间的所有天数,然后将它们过滤到所需的日期,例如1。

注意:,这可能不是最佳且有效的解决方案

答案 1 :(得分:0)

我不知道java.time API是否提供直接执行此操作的方法,但是您始终可以遍历开始和结束之间的所有日期,一一检查。

LocalDate start = LocalDate.of(2019, 1, 1);
LocalDate end = LocalDate.of(2022, 12, 31);
long daysBetween = ChronoUnit.DAYS.between(start, end);
System.out.println(daysBetween);
// you might want to use a parallel stream. It might speed things up a little
long count = LongStream.range(0, daysBetween + 1)
                 .mapToObj(start::plusDays)
                 .filter(x -> x.getDayOfMonth() == index)
                 .count();
System.out.println(count);

该月的最后一天有特殊情况,您需要:

.filter(x -> x.equals(x.with(TemporalAdjusters.lastDayOfMonth())))

答案 2 :(得分:0)

这是解决方法:

    LocalDate start = LocalDate.of(2019, 1, 1);
    LocalDate end = LocalDate.of(2019, 3, 31);
    long daysBetween = ChronoUnit.DAYS.between(start, end);

    int index = 1;
    int count = 0;
    start = start.minusDays(1);
    for(int i = 0; i <= daysBetween; i++){
        start = start.plusDays(1);
        if (start.getDayOfMonth() == index) {
            count++;
        }
    }
    System.out.println(count);

答案 3 :(得分:0)

使用以下方法找到之间的天数:

Days d = Days.daysBetween(startDate, endDate); int days = d.getDays();

查找特定的日期是否存在,假设11th of month.

(startDay + days)包含您需要这样的日子。

答案 4 :(得分:0)

    public long getNumberOfDayOfMonthBetweenDates(LocalDate startDate, LocalDate endDate, int dayOfMonth) {
    long result = -1;
    if (startDate != null && endDate != null && dayOfMonth > 0 && dayOfMonth < 32) {
        result = 0;
        LocalDate startDay = getDayInCurrentMonth(startDate, dayOfMonth);
        // add one day as end date is exclusive
        // add + 1 to cover higher possibilities (month and a half or half month or so)
        long totalMonths = ChronoUnit.MONTHS.between(startDate, endDate.plusDays(1)) + 2;
        for (int i = 0; i < totalMonths; i++) {
            if ((!startDay.isBefore(startDate) && startDay.isBefore(endDate)) || startDay.equals(startDate) || startDay.equals(endDate)) {
                result++;
            }
            startDay = getDayInCurrentMonth(startDay.plusMonths(1), dayOfMonth);
        }
    }
    return result;
}

private LocalDate getDayInCurrentMonth(LocalDate startDate, int dayOfMonth) {
    LocalDate dayOfThisMonth;
    try {
        dayOfThisMonth = startDate.withDayOfMonth(dayOfMonth);
    } catch (DateTimeException e) {
        // handle cases where current month does not contain the given day (example 30 in Feb)
        dayOfThisMonth = startDate.withDayOfMonth(startDate.lengthOfMonth());
    }
    return dayOfThisMonth;
}