DatePicker:仅 select 个星期一

DatePicker: Only select Mondays

我目前正在使用 Android 中的 DatePicker 让用户 select 约会。是否有可能只允许用户 select 星期一并禁用 selection 的所有其他日子?我没有在文档中找到任何内容。

目前我正在使用它来显示 DatePicker:

DatePickerDialog datePickerDialog = new DatePickerDialog(AddAppointment.this,
        new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year,
                                  int monthOfYear, int dayOfMonth) {

            // Do stuff with the info
            }
        }, setYear, setMonth, setDay);
datePickerDialog.show();

您可以尝试做一些不同的事情。首先,更改用户不想选择的日期的字体颜色(星期一除外),然后过滤选定的活动日并禁用功能,直到选择星期一。

您可以使用这个库 Material Date Time Picker,在这里您可以设置一个选项来显示特定日期,例如:

datePicker.setSelectableDays(Calendar[] days)

并将日历数组作为参数传递,其中包含所有可选择的日期。

要查找星期一数组,您可以使用以下逻辑:- Get all Fridays in a date Range in Java

您可以计算所选日期的日历周或使用以下方法之一计算最近的星期一。他们有评论,所以我保持文字简短。

public class ExampleDateCalculation {

    public static void main(String[] args) {
        int dayOfMonth = 4;
        int monthOfYear = 3;
        int year = 2018;

        // create a java.time.LocalDate of the given integers
        LocalDate localDate = LocalDate.of(year, monthOfYear, dayOfMonth);

        // calculate the calendar week of it
        int calendarWeekTheLocalDateIsIn = getCalendarWeek(localDate);

        // calculate the last Monday before this date
        LocalDate lastMonday = getLastFrom(DayOfWeek.MONDAY, localDate);

        // create a formatter for your locale
        DateTimeFormatter germanDateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");

        System.out.println(localDate.format(germanDateFormatter)
                + " is in calendar week "
                + calendarWeekTheLocalDateIsIn
                + " of the system locale and the last Monday before was at "
                + lastMonday.format(germanDateFormatter));
    }

    /**
     * <p>
     * Gets the calendar week number of the given {@link LocalDate} based on the
     * {@link Locale} of the operating system.
     * </p>
     * 
     * @param localDate the date of the day
     * @return the calendar week number the day is in
     */
    public static int getCalendarWeek(LocalDate localDate) {
        WeekFields weekFields = WeekFields.of(Locale.getDefault());
        return localDate.get(weekFields.weekOfWeekBasedYear());
    }

    /**
     * <p>
     * Gets the date of the last given weekday or day of week starting from the
     * weekday of the given date. The method calculates the date of the nearest
     * weekday chronologically backwards.
     * </p>
     * <p>
     * <strong>For example:</strong><br>
     * If the weekday of the given date is a Monday and the given day of week is a
     * Tuesday, then this method will return the date of the Tuesday before today,
     * which is 6 days back in the past.
     * </p>
     * 
     * @param weekday the day of week whose date is to be determined
     * @param from    the date to start from calculating backwards
     * @return the date of the last given day of week starting from the given date
     */
    public static LocalDate getLastFrom(DayOfWeek weekday, LocalDate from) {
        DayOfWeek fromWeekday = from.getDayOfWeek();
        int fromWeekdayValue = fromWeekday.getValue();
        int weekdaysValue = weekday.getValue();
        int daysToSubtract = 0;

        /*
         * Calculate the days to go back and be beware of negative values by means of
         * case differentiation. Get the positive difference by subtracting the smaller
         * value from the larger one and subtract a week if the result would be 0.
         */
        if (fromWeekdayValue < weekdaysValue) {
            daysToSubtract = 7 - (weekdaysValue - fromWeekdayValue);
        } else if (fromWeekdayValue > weekdaysValue) {
            daysToSubtract = fromWeekdayValue - weekdaysValue;
        } else {
            daysToSubtract = 7;
        }

        return from.minusDays(daysToSubtract);
    }
}

如果您希望用户只看到日历周或星期一,请按照 Uday Nayak 的回答中给出的建议进行操作。

If anyone finds errors in or knows disadvantages of this code, please let me know.