Java中两个日期之间的星期数 [英] Week numbers between two dates in Java

查看:404
本文介绍了Java中两个日期之间的星期数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是让两个日期范围之间的星期数。日期第24周的第24个秋天和日期第35周的第26个秋天。现在的问题是,如果我输入 2018-08-22T12:18:06,166 作为开始日期,我得到34,35,36。我不希望在这里看到36,因为结束日期不会在36周内。任何人都可以帮助我。此问题与此处提供的解决方案不同从开始日期到结束日期Java的周数。该解决方案存在一个我最近检测到的问题

My intention is to get the week numbers between two date ranges. The date 24th fall in the week number 34 and 26th fall in week number 35. Now the problem is if I put 2018-08-22T12:18:06,166 as the start date, i am getting 34,35,36. I am not expecting 36 here because the end date does not fall into week 36. Can anyone help me. This question is different from the solution provided here Week numbers from start date to end date Java . The solution has a problem which I detected recently

下面是获取该代码的代码:

The below is the code for getting it :

public static void main(String[] args) {
    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss','SSS");
    LocalDateTime startDate = LocalDateTime.parse("2018-08-24T12:18:06,166", format);
    LocalDateTime endDate = LocalDateTime.parse("2018-08-26T12:19:06,188", format);

    numberOfWeeks(startDate, endDate);

}

public static void numberOfWeeks(LocalDateTime startDate, LocalDateTime endDate) {
    int addWeek = 0;

    TemporalField tf = WeekFields.SUNDAY_START.weekOfYear();
    if (startDate.get(tf) < endDate.get(tf)) {
        addWeek = 1;
    }
    long weeks = WEEKS.between(startDate, endDate) + addWeek;
    List<String> numberWeeks = new ArrayList<>();
    if (weeks >= 0) {
        int week = 0;
        do {
            //Get the number of week
            LocalDateTime dt = startDate.plusWeeks(week);
            int weekNumber = dt.get(tf);
            numberWeeks.add(String.format("%d-W%d", dt.getYear(), weekNumber));
            week++;
        } while (week <= weeks);
    }
    System.out.println(numberWeeks);
}


推荐答案

public static void numberOfWeeks(LocalDateTime startDateTime, LocalDateTime endDateTime) {
    if (startDateTime.isAfter(endDateTime)) {
        throw new IllegalArgumentException("End date must not be before start date");
    }

    LocalDate endDate = endDateTime.toLocalDate();
    List<String> numberWeeks = new ArrayList<>();
    LocalDate currentDate = startDateTime.toLocalDate();
    while (currentDate.isBefore(endDate)) {
        numberWeeks.add(formatWeek(currentDate));
        currentDate = currentDate.plusWeeks(1);
    }
    // Now currentDate is on or after endDate, but are they in the same week?
    if (currentDate.get(WeekFields.SUNDAY_START.weekOfWeekBasedYear()) 
            == endDate.get(WeekFields.SUNDAY_START.weekOfWeekBasedYear())) {
        numberWeeks.add(formatWeek(currentDate));
    }

    System.out.println(numberWeeks);
}

public static String formatWeek(LocalDate currentDate) {
    return String.format("%d-W%d", 
            currentDate.get(WeekFields.SUNDAY_START.weekBasedYear()), 
            currentDate.get(WeekFields.SUNDAY_START.weekOfWeekBasedYear()));
}

使用 main 方法:


[2018-W34,2018-W35]

[2018-W34, 2018-W35]

我看到您已忽略链接问题中的其他答案 ,使用ThreeTen Extra库中的 YearWeek 的人。所以我以为你不想那样做。因此,我这几周一直使用 LocalDate

I see that you have ignored the other answer in the linked question, the one using YearWeek from the ThreeTen Extra library. So I assumed you didn’t want to do that. So I am using LocalDate for the weeks.

尽管有几个用户未能重现您的确切问题,我确实同意您在问题中的代码有缺陷。

While a couple of users have failed to reproduce your exact issue, I do agree that your code in the question is flawed.

这篇关于Java中两个日期之间的星期数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆