Android: 选中某月某周如何获取当月所有日期?

Android: how to get all the dates of the month after selecting the certain month and weekdays?

我制作了一个用于选择特定月份的微调器,然后我还创建了一些用于选择特定日期的复选框,那么我如何在选择这些信息后显示所有日期?

例如如果我选择一月并选择星期一和星期三,我如何显示星期一的所有日期:2/1,9/1/,16/1,23/1,30/1 和 Wednesdays:4/1,11 /1,18/1,25/1 分别?

期待您的来信!谢谢!

用这个方法你可以

private String getMondaysOfJanuary() 

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, Calendar.JANUARY); // month starts by 0 = january
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
        int month = cal.get(Calendar.MONTH);
        String output = "";
        while (cal.get(Calendar.MONTH) == month) {
            output += cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH)+1) + ",";
            cal.add(Calendar.DAY_OF_MONTH, 7);
        }
        return output.substring(0, output.length-1);
}

将这两行替换为您的数据并参数化此方法即可实现您的目标

cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);

避免遗留 date-time classes

旧的 DateCalendar classes 已被证明是令人困惑、有缺陷和麻烦的。现在是遗留的,在 Java 8 及更高版本中被 java.time classes 取代。请参阅下面的 back-ports 链接,了解早期 Java 和 Android.

使用java.time

which-days-of-the-week 复选框后面的数据可以使用 DayOfWeek 枚举表示。 class 提供了七个实例,一个对应一周中的每一天,Monday-Sunday。

您可以在 EnumSet, a special highly-optimized Set implementation for Enum object 秒内收集到用户想要的天数。

Set<DayOfWeek> daysOfWeek = EnumSet.noneOf( DayOfWeek.class );
daysOfWeek.add( DayOfWeek.MONDAY );
daysOfWeek.add( DayOfWeek.WEDNESDAY );

使用 YearMonth object 记住选择的月份和年份。

YearMonth ym = YearMonth.of( 2017 , Month.JANUARY );  // Or ( 2017 , 1 ) for January.

获取该月的第一个日期,表示为 LocalDateLocalDate class 表示没有 time-of-day 和时区的 date-only 值。

LocalDate firstOfMonth = ym.atDay( 1 );

在我们的 collection 中循环每个 day-of-week。对于每个,确定月份的第一次出现。 Java 为第一次出现提供了方便的 TemporalAdjuster implementation found in TemporalAdjusters。然后一次添加一个星期,直到我们找到下个月。

for ( DayOfWeek dayOfWeek : daysOfWeek ) {
    List<LocalDate> dates = new ArrayList<> ( 5 );
    LocalDate ld = firstOfMonth.with ( TemporalAdjusters.dayOfWeekInMonth ( 1 , dayOfWeek ) );
    do {
        dates.add ( ld );
        // Set up next loop.
        ld = ld.plusWeeks ( 1 );
    } while ( YearMonth.from ( ld ).equals ( ym ) );  // While in the targeted month.
    System.out.println ( "ym.toString(): " + ym + " | dayOfWeek.toString(): " + dayOfWeek + " | dates: " + dates );
}

ym.toString(): 2017-01 | dayOfWeek.toString(): MONDAY | dates: [2017-01-02, 2017-01-09, 2017-01-16, 2017-01-23, 2017-01-30]

ym.toString(): 2017-01 | dayOfWeek.toString(): WEDNESDAY | dates: [2017-01-04, 2017-01-11, 2017-01-18, 2017-01-25]

参见 this code live in IdeOne.com


关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

从哪里获得java.time classes?

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.