Click here to Skip to main content
15,077,377 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need help to fix this problem, i want a disable past day or future day in my app, here is my code
Java
final Calendar today = Calendar.getInstance();
        today.add(Calendar.DATE, -1);
        today.setTimeInMillis(System.currentTimeMillis() - 1000);

        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener(){
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                today.set(Calendar.YEAR, year);
                today.set(Calendar.MONTH, monthOfYear);
                today.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();

            }

            private void updateLabel() {
                String myFormat = "yyyy-MM-dd"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                editText.setText(sdf.format(today.getTime()));
            }
        };
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(sakit.this, date, today
                        .get(Calendar.YEAR), today.get(Calendar.MONTH),
                        today.get(Calendar.DAY_OF_MONTH)).show();
            }
        });


What I have tried:

I already disable a date from calendar and it doesn't work and it keeps red in datePicker
datePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
Posted
Comments
Gerry Schmitz 9-Oct-20 0:13am
   
Just default to "today".
Komang Putra 9-Oct-20 2:04am
   
if i want disable past date.. what should i do ?
David Crow 9-Oct-20 9:47am
   
What exactly do you mean by "disable past day or future day?"
Komang Putra 9-Oct-20 22:08pm
   
i mean disable past date or disable future date...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900