0

I have a datepicker in my application. I want to apply few condition while clicking on the date button first such condition is that it should disable weekends. I have come across answer of this question in the stackoverflow but it gives an error. Please, could you anyone help me. Thank you in advance

 {
        //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.getDatePicker().setMaxDate(System.currentTimeMillis()+1000);
            datepickerdialog.getDatePicker().setMinDate(System.currentTimeMillis()-10000);
            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
             );
            calendar.set(Calendar.HOUR, hour);
            calendar.set(Calendar.MINUTE, minute);

            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;*/

                setupDate(dayOfMonth,monthOfYear,year);
            }

        }
J. Vyas
  • 95
  • 1
  • 9
  • Check the date and time which you have passed to the datepicker dialog, you need to pass current date and time to get updated dialog – Madhu Oct 01 '15 at 05:05
  • date picker is year month day, you have it back to front. Check the order of the constructor arguements. http://developer.android.com/intl/ko/reference/android/app/DatePickerDialog.html#DatePickerDialog%28android.content.Context,%20android.app.DatePickerDialog.OnDateSetListener,%20int,%20int,%20int%29 – Breavyn Oct 01 '15 at 05:06

2 Answers2

1

As per the docs for the constructor:

public DatePickerDialog (
    Context context,
    DatePickerDialog.OnDateSetListener callBack,
    int year,
    int monthOfYear,
    int dayOfMonth)

It has to be year, month, day, not day, month, year as you have it.

As for the problem of your time being out by an hour, the first thing I'd be looking at is the default timezone. It's almost invariably something to do with that when the times are out by some exact number of hours.

It will either be because you're in a different timezone from your default, or you have daylight savings incorrectly configured.

To figure out your timezone, have a look at this question.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • friend thank you. Please, could you check what is wrong in my time code. It shows me one hour less than the current time. Thanks – J. Vyas Oct 01 '15 at 05:23
  • @J.Vyas, see the update, along with a look which should help in establishing whether the timezone is the problem. – paxdiablo Oct 01 '15 at 05:32
0

Try this:

private void showDatePickerDialog() {
        DatePickerDialog datepickerdialog = null;
        datepickerdialog = new DatePickerDialog
                (
                        this,
                       /* new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            } },*/
                        this,
                        year,
                        month,
                        day
                );
        datepickerdialog.show();
    }
Aakash
  • 5,181
  • 5
  • 20
  • 37