0

i have created a calendar where a suer can scroll through previous and next months calendar but the issue i am having is when the user tries to scroll into the previous month.

when the user clicks to the previous month the first time the app is run, it works but when the user clicks again it doesnt dispite the value i am sending to the Calendar.Set() being 100% correct and even debugged it yet the actual calendar doesnt update and therefore returns me the same month as the current one!

here is my code below.

@Override
public void onClick(View v) {

    // get current month

    int currentMonth = mCurrentMonth.get(Calendar.MONTH);
    Log.d(TAG, "day = " + mCurrentMonth.get(Calendar.DAY_OF_MONTH));

    Log.d(TAG, "currentMonth in onClick = " + currentMonth);
    if (v == mPreviousMonthButton) {
        Log.d(TAG, "mPreviousMonthButton CLICKED ");

        // if current month is january
        // decrement the current year and set month to december

        if (currentMonth == Calendar.JANUARY) {
            int currentYear = mCurrentMonth.get(Calendar.YEAR);
            mCurrentMonth.set(Calendar.YEAR, currentYear - 1);
            mCurrentMonth.set(Calendar.MONTH, Calendar.DECEMBER);
        } else {

            // else decrement the month

            Log.d(TAG, "currentMonth-- = " + currentMonth);
            mCurrentMonth.set(Calendar.MONTH, currentMonth);
            Log.d(TAG,
                    "month in previus button = "
                            + mCurrentMonth.get(Calendar.MONTH));

        }
        // save the month

        setDateForMonth();

    } else if (v == mNextMonthButton) {
        Log.d(TAG, "mNextMonthButton CLICKED ");

        if (currentMonth == Calendar.DECEMBER) {
            int currentYear = mCurrentMonth.get(Calendar.YEAR);
            mCurrentMonth.set(Calendar.YEAR, currentYear + 1);
            mCurrentMonth.set(Calendar.MONTH, Calendar.JANUARY);
        } else {
                                currentMonth--;
            mCurrentMonth.set(Calendar.MONTH, currentMonth + 1);
            Log.d(TAG, "currentMonth++ = " + currentMonth + 1);
            Log.d(TAG,
                    "month in next button = "
                            + mCurrentMonth.get(Calendar.MONTH));

        }

        // save the month

        setDateForMonth();

    }

}

here is the code that actually updates the UI. the problem is somwhere in the onClick as it returns the wrong month in the code below:

private void setDateForMonth() {

    monthList.clear();

    Log.d(TAG, ".........setDateForMonth...........");
    Log.d(TAG, "....................");

    Log.d(TAG, "month = " + mCurrentMonth.get(Calendar.MONTH));
    Log.d(TAG, "year = " + mCurrentMonth.get(Calendar.YEAR));

    CalendarMonth[] months = CalendarUtils
            .constructMonthViewArray(mCurrentMonth);

    for (int i = 0; i < months.length; i++) {
        monthList.add(months[i]);
        Log.d(TAG, monthList.get(i).getDay());
    }
    Log.d(TAG, "....................");

    mAdapter = new CalendarMonthAdapter(mContext, monthList);
    mMonthGridView.setAdapter(mAdapter);
    Months[] month = Months.values();

    String currentMonth = month[mCurrentMonth.get(Calendar.MONTH)]
            .toString();
    String year = Integer.toString(mCurrentMonth.get(Calendar.YEAR));
    mMonthLabel.setText(currentMonth + " " + year);

}

private enum Months { January, February, March, April, May, June, July, August, September, October, November, December };

Jono
  • 17,341
  • 48
  • 135
  • 217
  • 1
    That's not how you post code to Stackoverflow. Each line of code should just be indented by 4 spaces. – Pointy Jan 10 '11 at 17:26

4 Answers4

3

Unless I'm missing something, you never actually decrement the month, except when the month is January.

int currentMonth = mCurrentMonth.get(Calendar.MONTH);
...
if (currentMonth == Calendar.JANUARY) {
    int currentYear = mCurrentMonth.get(Calendar.YEAR);
    mCurrentMonth.set(Calendar.YEAR, currentYear - 1);
    mCurrentMonth.set(Calendar.MONTH, Calendar.DECEMBER);
} 
else {
    // else decrement the month
    Log.d(TAG, "currentMonth-- = " + currentMonth);
    mCurrentMonth.set(Calendar.MONTH, currentMonth);

You've got a comment that says to decrement the month, and even a log comment that says you should be decrementing the month... but no actual decrement :)

else {
    currentMonth--;
    mCurrentMonth.set(Calendar.MONTH, currentMonth);
robert_x44
  • 9,224
  • 1
  • 32
  • 37
  • i think i removed by mistake when i was trying something out but it originally had it their. i added back again and the date still doesnt change – Jono Jan 11 '11 at 09:01
  • When i first click on the previous button, it sets the month to 11(december) as it is january this month, when i click again, the currentMonth gets decremnted succesfully gtom 11 to 10 as shown on my log.d() i then set the Month to the current month of value 10 and it still doesnt set it properly at all when i later in my code do a log.d to get the current month set. – Jono Jan 11 '11 at 09:04
  • @jonney Your log shows that you set the current month to 10. What do you mean it doesn't set it properly? – robert_x44 Jan 11 '11 at 15:06
2

Drop java Calendar and move to JodaTime

dom farr
  • 4,041
  • 4
  • 32
  • 38
  • on the same line, drop java and move to c#? – KevinDTimm Jan 10 '11 at 17:42
  • @KevinDTimm. ha ha - now now, let's not get into that old chestnut. – dom farr Jan 10 '11 at 17:46
  • FYI, the [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [*java.time*](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. Much of *java.time* is back-ported to Java 6 & 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Mar 17 '18 at 22:01
1

To add or substract a month use mCalendar.add(Calendar.MONTH, 1); mCalendar.add(Calendar.MONTH, -1);

lomza
  • 9,412
  • 15
  • 70
  • 85
0

Its better to use date4J jar instead of Calendar or Date classes of Java.

Ali Ashraf
  • 1,841
  • 2
  • 24
  • 27