0

I'm trying to disable some days using setDisabledDays(Calendar[] days) method of wdullaer's material datetime picker, an alternative datetime picker for Android.

But I don't know how to pass the dates to the method using Calendar[]

abscanButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        datePickerDialog = DatePickerDialog.newInstance(MainActivity.this, Year, Month, Day);

        datePickerDialog.showYearPickerFirst(false);

        datePickerDialog.setMinDate(calendar);

        Calendar[] days = new Calendar[3];
        days[0] = // I don't know how to do this part
        datePickerDialog.setDisabledDays(days);

        datePickerDialog.show(getFragmentManager(), "DatePickerDialog");
    }
}

I successfully setMinDate() without any errors. I just need to know how to create the dates I will put in the Calendar array.

Note: I'm trying to add the dates manually.

pandd
  • 103
  • 4
meshachviktor
  • 45
  • 1
  • 6
  • Since the modern Java date and time API is so much nicer to work with, I’d add ThreeTenABP to my Android project and use `LocalDate` and it `atStartOfDay(ZoneId)` method and then `DateTimeUtils` for converting to `GregorianCalendar` that I’d fill into the array. – Ole V.V. Mar 08 '18 at 15:31

1 Answers1

0

To create the Calendar is easy:

Calendar cal = Calendar.getInstance();

This calendar will have the current date and time, at the JVM default timezone. If you want to set a different day, or month, or whatever, use the set method:

// set to March 5th 2018
cal.set(Calendar.DAY_OF_MONTH, 5);
cal.set(Calendar.MONTH, Calendar.MARCH);
cal.set(Calendar.YEAR, 2018);

In this API, months are zero-based (January is zero, February is 1, etc), so it's better to use the constants, such as Calendar.MARCH, to avoid any off-by-one errors.

This class is mutable, so always create a new instance with getInstance() before assigning it to your array.

Threeten Backport

As said in the comments, you can use this library - much nicer and compatible with the java.time API (this will make a future migration easier, as the java.time classes are available in newer Android API levels). See how to install/configure this lib here.

With this lib, it's even easier to create a specific date:

// March 5th 2018
LocalDate date = LocalDate.of(2018, Month.MARCH, 5);

Or, to get the current date:

LocalDate date = LocalDate.now();

Then, to convert to a Calendar, you must set it to a timezone and then convert:

Calendar cal = DateTimeUtils.toGregorianCalendar(date.atStartOfDay(ZoneId.systemDefault()));

It seems harder at first, but if you study this API, you'll soon see the benefits. Calendar follows a kind of one-size-fits-all design, but it's known to be troublesome and outdated. The new API provides different date/time types for each use case, and you must explicity convert between them to get what you need.

That's why the conversion code above seems so confusing, but once you get the idea, it becomes much easier than using Calendar.

pandd
  • 103
  • 4
  • I followed the first example but i keep getting a 'NullPointerException' error on clicking the button that triggers the displays of the datepicker view. – meshachviktor Mar 08 '18 at 17:08
  • 1
    https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – pandd Mar 08 '18 at 17:11
  • i have fixed the NullPointerException Issue. That was my fault. Like i said earlier, I followed your first example and the activity does not crash anymore. plus it works! Thanks a bunch, pandd! – meshachviktor Mar 08 '18 at 19:16
  • There’s nothing inherent in the first example that will give you a `NullPointerException`, so it must be the way you are using it. I do, however, very warmly recommend the second example, the ThreeTenABP approach. – Ole V.V. Mar 08 '18 at 19:33