DatePicker:仅选择星期一 [英] DatePicker: Only select Mondays

查看:344
本文介绍了DatePicker:仅选择星期一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前在Android中使用 DatePicker 来让用户选择日期。是否有可能只允许用户选择星期一,而禁用所有其他日期进行选择?我没有在文档中找到任何内容。

I am currently using the DatePicker in Android to let the user select a date. Is there a possibility to only allow the user to select Mondays and disable all other days for selection? I did not find anything in the Documentation.

当前,我正在使用它来显示 DatePicker

Currently I am using this to show the 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();


推荐答案

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

You can calculate the calendar week of the chosen date or calculate the most recent Monday using one of the methods below. They are commented, so I keep the text short.

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 you want the user to only see calendar weeks or Mondays, follow the suggestions given in Uday Nayak's answer.


如果有人发现此代码有错误或知道此代码的缺点,请告诉我。

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

这篇关于DatePicker:仅选择星期一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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