关于周末的Datepicker

时间:2015-10-06 22:32:18

标签: java android

我有显示日期的按钮。在这个,如果我选择周末的日期,那么我希望它应该立即提示消息。 我运行的代码只检查myDay == 2。如果我选择星期六,不是myDay == 1。当我选择周末时,我应该显示当前日期而不是选择的周末日期。

private void setupDate(int day, int month, int year) {

        Calendar cal = Calendar.getInstance();
        cal.set(day, month, year);
        int myDay = cal.get(Calendar.DAY_OF_WEEK);
        if (myDay == 1 || myDay == 2) {
            Toast.makeText(DateTimePickerActivity.this, "My good day is  " + myDay, Toast.LENGTH_SHORT).show();
        } else {
            String strMonth = ((month + 1) <= 9) ? ("0" + (month + 1)) : String.valueOf(month + 1);
            views.button_date.setText(String.valueOf(day) + "/" + strMonth + "/" + String.valueOf(year));
        }


    }

    private void showDatePickerDialog() {
        DatePickerDialog datepickerdialog = new DatePickerDialog
                (
                        this,
                         dpListenerView,
                       /* new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            } },*/
                        //this,
                        x_year,
                        month,
                        day
                );
        datepickerdialog.show();
    }

   class ButtonViews {
        Button button_time;
        Button button_date;

        public ButtonViews() {
            button_date = (Button) findViewById(R.id.button_date);
            button_time = (Button) findViewById(R.id.button_time);
        }
    }

  class dpListener implements OnDateSetListener {
      @Override
      public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

              setupDate(dayOfMonth, monthOfYear, year);

      }

   }

1 个答案:

答案 0 :(得分:0)

我认为你对Calendar.DAY_OF_WEEK

感到困惑
  1. 星期日= 1
  2. 星期一= 2
  3. 星期二= 3
  4. 星期三= 4
  5. 星期四= 5
  6. 星期五= 6
  7. 星期六= 7

    使用,

    if(myDay == 1 || myDay == 7){}