Domanda

I am still trying to create a monthview with calendarView.

My problem is that I have no idea how I could switch the month of calendarView. I want to click a button and then the view shall display the next or last month. The view is alredy grounded, so you cant scroll through.

CalendarView cal = (CalendarView) findViewById(R.id.calendarMonthView);
    cal.setEnabled(false);

My idea is to do it with following Code:


long dateOfLastMonth = currentMonth - month; // pseudo code

            CalendarView lastMonth = (CalendarView) findViewById(R.id.calendarMonthView);
            lastMonth.setDate(**dateOfLastMonth**);

But I have no idea what value dateOfLastMonth should have.

Is that the right way? What value should dateOfLastMoth (no idea because it must be a long) have? Is there another way?

È stato utile?

Soluzione

Try this:

Calendar cal = Calendar.getInstance(); // Calendar object, with time set to now
cal.add(Calendar.MONTH, -1); // subtract a month
long dateOfLastMonth = cal.getTimeInMillis(); // get the milliseconds since 01/01/1970

If you want the 1st of the month do this

cal.set(Calendar.DAY_OF_MONTH, 1);

I suggest to do that before subtracting a month, because I don't know what happens if today is the 31st of the month and the previous month doesn't have a 31st.

Edit: It wouldn't be a problem if it's the 31st, I just tried it and the Calendar just sets to the 30th of the previous month

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top