【问题标题】:How to do get list of calendar dates in array from two calendar dates in java如何从java中的两个日历日期获取数组中的日历日期列表
【发布时间】:2018-06-18 04:57:20
【问题描述】:

我试图找出两个特定日期的日历日期列表。但我只能在同一年内确定两个日期。

我做了什么:

    private ArrayList<Calendar> weekendList = null;
    public void findWeekendsList(long startDate, long endDate)
    {
        weekendList = new ArrayList();
        Calendar calendarStart = null;
        Calendar calendarEnd=null;

        calendarStart= Calendar.getInstance();
        calendarStart.setTimeInMillis(startDate);



        /*calendarEnd= Calendar.getInstance();
        calendarEnd.setTimeInMillis(endDate);
       */

        // The while loop ensures that you are only checking dates in the current year

       while(calendar.get(Calendar.YEAR) == Calendar.getInstance().get(Calendar.YEAR)){
            // The switch checks the day of the week for Saturdays and Sundays
            switch(calendar.get(Calendar.DAY_OF_WEEK)){
                case Calendar.SATURDAY:
                case Calendar.SUNDAY:

                    weekendList.add(calendar);
                    break;
            }

            // Increment the day of the year for the next iteration of the while loop
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }
    }

我有 102 个周末,就像 1 月 9 日到 12 月 31 日,但我想从今天(2018 年 1 月 9 日)到明年(比如 2019 年 1 月 9 日),所有周末都在 arrayList 中。

如果有人有任何想法,那对我会有很大帮助。

【问题讨论】:

  • 取当前的Date,将年份增加1,这将成为目标结束日期——类似于this——但我会使用更新的日期/时间API跨度>
  • 您的while 条件似乎错误,应该更像while (calendar.getTime().before(toDate)) {,其中toDate 在您要检查的最后一个Date 之后
  • 另外,因为Calendar 是可变的,添加calendar 将简单地生成一个具有相同日期的值列表
  • 另外,从技术上讲,因为您同时添加了 SaturdaySunday,所以您每个“周末”的天数会减少

标签: java datetime calendar weekday


【解决方案1】:

因此,您马上就会遇到许多突出的问题...

  • 使用过时的 API。 Calendar 应该不惜一切代价避免,它很麻烦,很笨拙,而且很容易搞砸
  • 只要年份相同,while 循环就会循环,因此它完全忽略了 endDate
  • 因为Calendar 是可变的,所以您生成的只是List,其中包含相同的日期/时间值
  • 您的算法将周末视为“星期六”或“星期日”,通常情况下,我认为周末是“星期五”和“星期一”之间的时间 - 不知道这是否是故意的你的部分,但它对我来说很突出。

由于 Java 8 包含更新的日期/时间 API,您应该开始使用它。如果你没有使用 Java 8+(我会问一些严肃的问题为什么不使用),你应该使用更可靠的 API - JodaTime 浮现在脑海中,但也有一个 compatible back port of the Java 8 Date/Time for earlier JDK/JVMs

现在,说了这么多,我会做一些更像......

public List<LocalDate> findWeekendsBetween(long startTime, long endTime) {
    LocalDate startDate = LocalDate.ofEpochDay(startTime);
    LocalDate endDate = LocalDate.ofEpochDay(endTime);

    System.out.println("Starting from " + startDate);
    System.out.println("Ending at " + endDate);

    List<LocalDate> weekends = new ArrayList<>(25);
    while (startDate.isBefore(endDate) || startDate.equals(endDate)) {
        switch (startDate.getDayOfWeek()) {
            case SATURDAY:
                weekends.add(startDate);
                startDate = startDate.plusDays(2);
                break;
            default:
                startDate = startDate.plusDays(1);
        }
    }
    return weekends;
}

事实上,我什至会更改它,要求调用者向您传递LocalDate 值...

public List<LocalDate> findWeekendsBetween(LocalDate startDate, LocalDate endDate) {

因为long 是模棱两可的。

上述算法包含startTimeendTime,并认为“周末”是“星期五”和“星期一”之间的时间,所以它只会返回“星期六”值

然后你可以使用类似...的方式调用它

LocalDate startDate = LocalDate.now();
LocalDate endDate = startDate.plusMonths(1);

List<LocalDate> weekends = findWeekendsBetween(startDate.toEpochDay(), endDate.toEpochDay());
System.out.println("Found " + weekends.size() + " Saturday/Sundays");
for (LocalDate date : weekends) {
    System.out.println(date);
}

(如您所见,我懒得计算固定时间点,只使用LocalDate并将其转换为long值)

在我的测试中打印出类似...

Starting from 2018-01-09
Ending at 2018-02-09
Found 4 Weekends
2018-01-13
2018-01-20
2018-01-27
2018-02-03

但是您只添加了星期六,它也必须是星期日。

是的,正如我所说,我让它只接受Saturday,至于我,周末包括SaturdaySunday

只需将SUNDAY 包含到switch 语句中即可轻松解决...

switch (startDate.getDayOfWeek()) {
    case SATURDAY:
    case SUNDAY:
        weekends.add(startDate);
        break;
    default:
        startDate = startDate.plusDays(1);
}

我认为,它可能是 52*2=104 列表大小。

我没有测试一整年,我只检查了一个月的值。您可以更改结束日期,例如...

LocalDate endDate = startDate.plusYears(1);

【讨论】:

  • 非常感谢您的宝贵回答,非常感谢。
  • 非常感谢您的回答,但您只添加了星期六,也必须是星期日。我认为,它可能是 52*2=104 列表大小。
  • 对于 Java 6 和 Java 7,java.time 的大部分功能都可以在 ThreeTen-Backport 项目中找到。对于早期的 Android,请参阅 ThreeTenABP 项目。 Joda-Time 项目现在处于维护模式,其团队建议迁移到 java.time 类(或后端)。
  • @No_Name_Code 正如我所说,我只是捕捉星期六,如果这对你很重要,你很难把星期天包括在内。在我的示例中,我只扫描一个月,您认为更改为捕获一年对您来说有多难?
  • @BasilBourque 我确实提到过,也许不是名字
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多