如何从java中的两个日历日期获取数组中的日历日期列表

时间:2018-01-09 07:41:15

标签: java datetime calendar weekday

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

我做了什么:

    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。

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

1 个答案:

答案 0 :(得分:3)

所以,立即,你有很多问题突出......

  • 使用过时的API。 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.constraint.ConstraintLayout android:id="@+id/first_row" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintDimensionRatio="4:1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <com.my.app.presentation.component.FilterObjectTypeWithImageView android:id="@+id/all_background_filter_view" android:layout_width="0dp" android:layout_height="0dp" custom:text="@string/filter_all" android:visibility="invisible" app:layout_constraintDimensionRatio="1:1" app:layout_constraintHorizontal_chainStyle="spread" app:layout_constraintEnd_toStartOf="@id/stay_filter_view" app:layout_constraintStart_toStartOf="@id/first_row" app:layout_constraintTop_toTopOf="@id/first_row" app:layout_constraintBottom_toBottomOf="@id/first_row"/> <com.my.app.presentation.component.FilterObjectTypeIconlessView android:id="@+id/all_filter_view" android:layout_width="0dp" android:layout_height="0dp" custom:text="@string/filter_all" app:layout_constraintStart_toStartOf="@id/all_background_filter_view" app:layout_constraintEnd_toEndOf="@id/all_background_filter_view" app:layout_constraintTop_toTopOf="@id/all_background_filter_view" app:layout_constraintBottom_toBottomOf="@id/all_background_filter_view"/> <com.my.app.presentation.component.FilterObjectTypeWithImageView android:id="@+id/stay_filter_view" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintDimensionRatio="1:1" app:layout_constraintEnd_toStartOf="@id/restaurants_filter_view" app:layout_constraintStart_toEndOf="@id/all_background_filter_view" app:layout_constraintTop_toTopOf="@id/first_row" app:layout_constraintBottom_toBottomOf="@id/first_row" custom:src="@drawable/icon__accomodation__x3" custom:text="@string/filter_stay" /> <com.my.app.presentation.component.FilterObjectTypeWithImageView android:id="@+id/restaurants_filter_view" android:layout_width="0dp" android:layout_height="0dp" custom:text="@string/filter_restaurants" custom:src="@drawable/icon__restaurant__x3" app:layout_constraintDimensionRatio="1:1" app:layout_constraintStart_toEndOf="@id/stay_filter_view" app:layout_constraintEnd_toStartOf="@id/shops_filter_view" app:layout_constraintTop_toTopOf="@id/first_row" app:layout_constraintBottom_toBottomOf="@id/first_row" /> <com.my.app.presentation.component.FilterObjectTypeWithImageView android:id="@+id/shops_filter_view" android:layout_width="0dp" android:layout_height="0dp" custom:text="@string/filter_shops" custom:src="@drawable/icon__shop__x3" app:layout_constraintDimensionRatio="1:1" app:layout_constraintStart_toEndOf="@id/restaurants_filter_view" app:layout_constraintTop_toTopOf="@id/first_row" app:layout_constraintBottom_toBottomOf="@id/first_row" app:layout_constraintEnd_toEndOf="@id/first_row"/> <android.support.constraint.ConstraintLayout android:id="@+id/second_row" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintDimensionRatio="4:1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/first_row" /> <com.my.app.presentation.component.FilterObjectTypeWithImageView android:id="@+id/sights_filter_view" android:layout_width="0dp" android:layout_height="0dp" custom:text="@string/filter_sights" custom:src="@drawable/icon__sights__x3" app:layout_constraintHorizontal_chainStyle="spread" app:layout_constraintDimensionRatio="1:1" app:layout_constraintStart_toStartOf="@id/second_row" app:layout_constraintEnd_toStartOf="@id/sports_filter_view" app:layout_constraintTop_toTopOf="@id/second_row" app:layout_constraintBottom_toBottomOf="@id/second_row" /> <com.my.app.presentation.component.FilterObjectTypeWithImageView android:id="@+id/sports_filter_view" android:layout_width="0dp" android:layout_height="0dp" custom:text="@string/filter_sports" custom:src="@drawable/icon__sport__x3" app:layout_constraintDimensionRatio="1:1" app:layout_constraintStart_toEndOf="@id/sights_filter_view" app:layout_constraintEnd_toStartOf="@id/educational_filter_view" app:layout_constraintTop_toTopOf="@id/second_row" app:layout_constraintBottom_toBottomOf="@id/second_row"/> <com.my.app.presentation.component.FilterObjectTypeWithImageView android:id="@+id/educational_filter_view" android:layout_width="0dp" android:layout_height="0dp" custom:text="@string/filter_educational" custom:src="@drawable/icon__study__x3" app:layout_constraintDimensionRatio="1:1" app:layout_constraintStart_toEndOf="@id/sports_filter_view" app:layout_constraintEnd_toStartOf="@id/public_filter_view" app:layout_constraintTop_toTopOf="@id/second_row" app:layout_constraintBottom_toBottomOf="@id/second_row"/> <com.my.app.presentation.component.FilterObjectTypeWithImageView android:id="@+id/public_filter_view" android:layout_width="0dp" android:layout_height="0dp" custom:text="@string/filter_public" custom:src="@drawable/icon__public__x3" app:layout_constraintDimensionRatio="1:1" app:layout_constraintEnd_toEndOf="@id/second_row" app:layout_constraintTop_toTopOf="@id/second_row" app:layout_constraintStart_toEndOf="@id/educational_filter_view" app:layout_constraintBottom_toBottomOf="@id/second_row"/> <View android:id="@+id/all_filter_top_border" android:layout_width="0dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toStartOf="@id/all_filter_view" app:layout_constraintEnd_toStartOf="@id/stay_filter_view" app:layout_constraintTop_toTopOf="parent"/> <View android:id="@+id/all_filter_bottom_border" android:layout_width="0dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toStartOf="@id/all_filter_view" app:layout_constraintEnd_toStartOf="@id/stay_filter_view" app:layout_constraintTop_toBottomOf="@id/all_filter_view"/> <View android:id="@+id/stay_filter_top_border" android:layout_width="0dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toStartOf="@id/stay_filter_view" app:layout_constraintEnd_toStartOf="@id/restaurants_filter_view" app:layout_constraintTop_toTopOf="@id/stay_filter_view"/> <View android:id="@+id/stay_filter_bottom_border" android:layout_width="0dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toStartOf="@id/stay_filter_view" app:layout_constraintEnd_toStartOf="@id/restaurants_filter_view" app:layout_constraintTop_toBottomOf="@id/stay_filter_view"/> <View android:id="@+id/restaurants_filter_top_border" android:layout_width="0dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toStartOf="@id/restaurants_filter_view" app:layout_constraintEnd_toStartOf="@id/shops_filter_view" app:layout_constraintTop_toTopOf="parent"/> <View android:id="@+id/restaurants_filter_bottom_border" android:layout_width="0dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toStartOf="@id/restaurants_filter_view" app:layout_constraintEnd_toStartOf="@id/shops_filter_view" app:layout_constraintTop_toBottomOf="@id/restaurants_filter_view"/> <View android:id="@+id/shops_filter_top_border" android:layout_width="0dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toStartOf="@id/shops_filter_view" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"/> <View android:id="@+id/shops_filter_bottom_border" android:layout_width="0dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toStartOf="@id/shops_filter_view" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/shops_filter_view"/> <View android:id="@+id/sights_filter_bottom_border" android:layout_width="0dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toStartOf="@id/sights_filter_view" app:layout_constraintEnd_toStartOf="@id/sports_filter_view" app:layout_constraintBottom_toBottomOf="@id/sights_filter_view"/> <View android:id="@+id/sports_filter_bottom_border" android:layout_width="0dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toStartOf="@id/sports_filter_view" app:layout_constraintEnd_toStartOf="@id/educational_filter_view" app:layout_constraintBottom_toBottomOf="@id/sports_filter_view"/> <View android:id="@+id/educational_filter_bottom_border" android:layout_width="0dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toStartOf="@id/educational_filter_view" app:layout_constraintEnd_toStartOf="@id/public_filter_view" app:layout_constraintBottom_toBottomOf="@id/educational_filter_view"/> <View android:id="@+id/public_filter_bottom_border" android:layout_width="0dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toStartOf="@id/public_filter_view" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="@id/public_filter_view"/> <View android:id="@+id/all_filter_left_border" android:layout_width="1dp" android:layout_height="0dp" android:background="@color/filterUnactivatedColor" app:layout_constraintTop_toTopOf="@id/all_filter_view" app:layout_constraintBottom_toBottomOf="@id/all_filter_view" app:layout_constraintStart_toStartOf="@id/all_filter_view"/> <View android:id="@+id/sights_filter_left_border" android:layout_width="1dp" android:layout_height="0dp" android:background="@color/filterUnactivatedColor" app:layout_constraintTop_toTopOf="@id/sights_filter_view" app:layout_constraintBottom_toBottomOf="@id/sights_filter_view" app:layout_constraintStart_toStartOf="@id/sights_filter_view"/> <View android:id="@+id/stay_filter_left_border" android:layout_width="1dp" android:layout_height="0dp" android:background="@color/filterUnactivatedColor" app:layout_constraintTop_toTopOf="@id/stay_filter_view" app:layout_constraintBottom_toTopOf="@id/sports_filter_view" app:layout_constraintStart_toStartOf="@id/stay_filter_view"/> <View android:id="@+id/sports_filter_left_border" android:layout_width="1dp" android:layout_height="0dp" android:background="@color/filterUnactivatedColor" app:layout_constraintTop_toTopOf="@id/sports_filter_view" app:layout_constraintBottom_toBottomOf="@id/sports_filter_view" app:layout_constraintStart_toStartOf="@id/sports_filter_view"/> <View android:id="@+id/restaurants_filter_left_border" android:layout_width="1dp" android:layout_height="0dp" android:background="@color/filterUnactivatedColor" app:layout_constraintTop_toTopOf="@id/restaurants_filter_view" app:layout_constraintBottom_toTopOf="@id/educational_filter_view" app:layout_constraintStart_toStartOf="@id/restaurants_filter_view"/> <View android:id="@+id/educational_filter_left_border" android:layout_width="1dp" android:layout_height="0dp" android:background="@color/filterUnactivatedColor" app:layout_constraintTop_toTopOf="@id/educational_filter_view" app:layout_constraintBottom_toBottomOf="@id/educational_filter_view" app:layout_constraintStart_toStartOf="@id/educational_filter_view"/> <View android:id="@+id/shops_filter_left_border" android:layout_width="1dp" android:layout_height="0dp" android:background="@color/filterUnactivatedColor" app:layout_constraintTop_toTopOf="@id/shops_filter_view" app:layout_constraintBottom_toTopOf="@id/public_filter_view" app:layout_constraintStart_toStartOf="@id/shops_filter_view"/> <View android:id="@+id/public_filter_left_border" android:layout_width="1dp" android:layout_height="0dp" android:background="@color/filterUnactivatedColor" app:layout_constraintTop_toTopOf="@id/public_filter_view" app:layout_constraintBottom_toBottomOf="@id/public_filter_view" app:layout_constraintStart_toStartOf="@id/public_filter_view"/> <View android:id="@+id/shops_filter_right_border" android:layout_width="1dp" android:layout_height="0dp" android:background="@color/filterUnactivatedColor" app:layout_constraintTop_toTopOf="@id/shops_filter_view" app:layout_constraintBottom_toBottomOf="@id/shops_filter_view" app:layout_constraintEnd_toEndOf="@id/shops_filter_view"/> <View android:id="@+id/public_filter_right_border" android:layout_width="1dp" android:layout_height="0dp" android:background="@color/filterUnactivatedColor" app:layout_constraintTop_toTopOf="@id/public_filter_view" app:layout_constraintBottom_toBottomOf="@id/public_filter_view" app:layout_constraintEnd_toEndOf="@id/public_filter_view"/> <View android:id="@+id/first_border_center_pixel" android:layout_width="1dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toEndOf="@id/all_filter_bottom_border" app:layout_constraintBottom_toBottomOf="@id/all_filter_bottom_border" /> <View android:id="@+id/second_border_center_pixel" android:layout_width="1dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toEndOf="@id/stay_filter_bottom_border" app:layout_constraintBottom_toBottomOf="@id/stay_filter_bottom_border" /> <View android:id="@+id/third_border_center_pixel" android:layout_width="1dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintStart_toEndOf="@id/restaurants_filter_bottom_border" app:layout_constraintBottom_toBottomOf="@id/restaurants_filter_bottom_border" /> <View android:id="@+id/fourth_border_center_pixel" android:layout_width="1dp" android:layout_height="1dp" android:background="@color/filterUnactivatedColor" app:layout_constraintEnd_toEndOf="@id/shops_filter_bottom_border" app:layout_constraintBottom_toBottomOf="@id/shops_filter_bottom_border" /> </android.support.constraint.ConstraintLayout> 应该不惜一切代价避免,它很麻烦,笨拙且容易搞砸
  • Calendar循环仅在年份相同的情况下循环,因此它完全忽略while
  • 由于endDate是可变的,因此您生成的所有内容都是Calendar,其中包含相同的日期/时间值
  • 你算法认为周末是&#34;星期六&#34;或者&#34;星期天&#34;通常情况下,我认为周末是&#34;星期五&#34;和&#34;星期一&#34; - 不知道这是否是你的故意,但它对我来说很突出。

由于Java 8包含更新的Date / Time API,因此您应该开始使用它。如果您不使用Java 8+(并且我会问一些严重的问题,为什么不这样做),您应该使用更可靠的API - JodaTime浮现在脑海中,但也有一个{{ 3}}

现在,说了这么多,我做的更像是......

List

事实上,我甚至会更改它,以便呼叫者必须通过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) { 不明确。

上述算法包含longstartTime,并考虑周末&#34;是&#34;星期五和#34;之间的时间和&#34;星期一&#34;,所以它只会返回&#34;星期六&#34;值

然后你可以使用像......这样的东西来调用它。

endTime

(正如您所见,我懒得计算一个固定的时间点并使用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 ,对我而言,周末包含SaturdaySaturday

只需将Sunday包含在SUNDAY声明中即可轻松修复...

switch
  

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

我没有测试一整年,我只检查了一个月的价值。你可以改变结束日期,比如......

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