TheDevMan
TheDevMan

Reputation: 5954

CalendarView in Android not showing correct month

I have implemented CalendarView in my Android App. I have a strange issue on selection of the date. The month is showing April 2015 in the top but the toast is showing 1/3/2015? If I Scroll May 2015 the toast shows 1/4/2015. The month is always a previous month.

I am doing something like this:

calendar =(CalendarView) findViewById(R.id.calendarforstart);
calendar.setShowWeekNumber(false);
    calendar.setFirstDayOfWeek(2);
    calendar.setOnDateChangeListener(new OnDateChangeListener() 
    {
        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int day) 
        {
            Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
        }
    });

Please check the snapshot:

enter image description here

Did you notice in the top it shows April 2015 but the below the toast shows 1/3/2015.. Why is this happening?

Can anybody help me fix this?

Thanks!

Upvotes: 0

Views: 1771

Answers (1)

Fahim
Fahim

Reputation: 12358

Month start with 0. so jan-dec is like 0-11

you need to change it to month+1

Toast.makeText(getApplicationContext(), day + "/" + (month+1) + "/" + year, Toast.LENGTH_LONG).show();

Upvotes: 5

Related Questions