Search code examples
androidandroid-calendarcalendarview

How to show a specific month in android CalendarView?


I want to show a specific month to user using android CalendarView. Please help me to implement this functionality. I wrote following code but it is not working.

  CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
    Calendar calendar1 = Calendar.getInstance();
    calendar1.set(2016, 6, 1);

    Calendar calendar2 = Calendar.getInstance();
    calendar2.set(2016, 6, 30);

    calendarView.setMinDate(calendar1.DATE);
    calendarView.setMaxDate(calendar2.DATE);

Following output is coming when i am trying to run the app using above code.

enter image description here


Solution

  • Hint: What is the the value of Calendar.DATE? Documentation says 5, and so when you call this method, you are saying "5 milliseconds past Jan 01, 1970."

    setMinDate(long minDate)

    Sets the minimal date supported by this CalendarView in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.

    Basically, that is a static field, and probably not the value that you want.

    Perhaps you want getTimeInMillis() which returns the long for the value that you set the date at?

    calendarView.setMinDate(calendar1.getTimeInMillis());
    calendarView.setMaxDate(calendar2.getTimeInMillis());