Home > Net >  How to set maximum date upto current month only in Datepicker?
How to set maximum date upto current month only in Datepicker?

Time:03-07

I want to set max date upto current month only. But if I use below concept app crashes if date exceeds current month.

public void showDateTimePicker() {
        final Calendar currentDate = Calendar.getInstance();
        Calendar date = Calendar.getInstance();
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                date.set(year, monthOfYear, dayOfMonth);

                //String date = null;
                if (monthOfYear <= 9){
                    month = "0"   (monthOfYear 1);
                }else {
                    month = ""   (monthOfYear 1);
                }

                if (dayOfMonth <= 9){
                    day = "0"   dayOfMonth;
                }else {
                    day = ""   dayOfMonth;
                }

                String date = year   "-"   month   "-"   day;
                appointment_date = date;
                edt_appointment_date.setText(AppUtils.getInstance(context).getFormattedDate2(date));
            }
        }, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE));
        datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
        datePickerDialog.getDatePicker().setMaxDate((System.currentTimeMillis() - 1000)   (1000*60*60*24*7));
        datePickerDialog.show();
    }

Crash as below:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.SimpleMonthView.getMonthHeight()' on a null object reference at android.widget.DayPickerView.onLayout(DayPickerView.java:232) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at android.widget.ScrollView.onLayout(ScrollView.java:1660) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673) at android.widget.LinearLayout.onLayout(LinearLayout.java:1582) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at com.android.internal.widget.AlertDialogLayout.setChildFrame(AlertDialogLayout.java:358) at com.android.internal.widget.AlertDialogLayout.onLayout(AlertDialogLayout.java:351) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332) at android.widget.FrameLayout.onLayout(FrameLayout.java:270) at com.android.internal.policy.DecorView.onLayout(DecorView.java:784) at android.view.View.layout(View.java:22844) at android.view.ViewGroup.layout(ViewGroup.java:6389) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3470) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2938) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972) at android.view.Choreographer.doCallbacks(Choreographer.java:796) at android.view.Choreographer.doFrame(Choreographer.java:731) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

How to achieve this. Any help would be appreciable.

CodePudding user response:

No idea about the NullPointerException you are getting looks like a device specific crash.

To restrict user to select date only for current month thecode below should work fine . Also the last argument of constructor is dayOfMonth so u should fix it also it should be currentDate.get(Calendar.DAY_OF_MONTH).

public void showDateTimePicker() {
    final Calendar currentDate = Calendar.getInstance();
    DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        }
    }, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH));
    datePickerDialog.getDatePicker().setMinDate(currentDate.getTimeInMillis());
    currentDate.set(Calendar.DAY_OF_MONTH, currentDate.getActualMaximum(Calendar.DAY_OF_MONTH));
    datePickerDialog.getDatePicker().setMaxDate(currentDate.getTimeInMillis());
    datePickerDialog.show();
}

To restrict it to current week you can use .

currentDate.set(Calendar.DAY_OF_WEEK, currentDate.getActualMaximum(Calendar.DAY_OF_WEEK));

Look into Calendar class it has all the methods and fields to manipulate the date .

  • Related