我已经在日期选择器中编写了代码。它工作正常。现在我想添加一个功能。不管是工作日还是周末。我想要一些指导,需要使用哪种方法来检查我选择的日期,也可以是周末或工作日。拜托,任何人都可以检查我的密码并指引我。我们将不胜感激。提前谢谢。

public class DateTimePickerActivity extends AppCompatActivity implements
        View.OnClickListener,
       TimePickerDialog.OnTimeSetListener


      // TimePickerDialog.OnTimeSetListener
{
    //Declaration for class
    ButtonViews views;
    dpListener dpListenerView;
    // Declartion for member vairables
    int day, month, x_year;
    int hour;
    int minute;
    Calendar calendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        views = new ButtonViews();
        dpListenerView = new dpListener();

        //ButtonListener
        views.button_date.setOnClickListener(this);
        views.button_time.setOnClickListener(this);
        //

       // pick up the default date using Calender class
         calendar =  GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());

        day = calendar.get(Calendar.DAY_OF_MONTH);
        month = calendar.get(Calendar.MONTH);
        x_year = calendar.get(Calendar.YEAR);

        hour = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);

        setupDate(day, month, x_year);
        setupTime(hour, minute);
   }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_date:
                showDatePickerDialog();
                break;
            case R.id.button_time:
                  showTimePickerDialog();
                break;
        }
    }


    private void setupTime(int hours, int minutes) {
        views.button_time.setText(hours + ":" + minutes);
    }

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

        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();
    }


 /*   private OnDateSetListener dpListener = new OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
               *//* day = dayOfMonth;
                month = monthOfYear;
                x_year = year;*//*
                setupDate(dayOfMonth,monthOfYear,year);
            }

        };*/


    public void showTimePickerDialog() {

      TimePickerDialog   timePickerDialog = new TimePickerDialog(
                DateTimePickerActivity.this,
                this,
                hour,
                minute,
                true
         );


        timePickerDialog.show();
    }

//    @Override
//    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//        setupDate(dayOfMonth,monthOfYear,year);
//    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        setupTime(hourOfDay, minute);
    }


   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) {
                day = dayOfMonth;
                month = monthOfYear;
                x_year = year;

            int i = calendar.get(Calendar.DAY_OF_WEEK);
            switch (i) {
                case 1:
                    Toast.makeText(DateTimePickerActivity.this
                            , "You have selected " + calendar.SUNDAY
                            , Toast.LENGTH_SHORT);
                    break;

                default:
                    setupDate(dayOfMonth,monthOfYear,year);
            }
        }

    }
}

最佳答案

尝试此代码

Calendar yourCalendar;

int dayOfWeek = yourCalendar.get(Calendar.DAY_OF_WEEK); // it will returns 1 (Calendar.SUNDAY) to 7 (Calendar.SATURDAY)

那你应该检查一下
If(dayOfWeek == 7 || dayOfWeek == 1){
  // as your requirement: you should display mesage they can not select the weekend" here
  // then you set the value in datepickerdialog by current date
  return "weekend";
}else{
  return "week date";
}

关于java - 如何查看我选择的是星期几,是否是周末,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32961537/

10-10 04:34