javadate

How to handle dates in java


I have a java programe which I can set a date in yyyy-MM-dd and I can get next and previous dates from it. following methods do the work,,

public void setDate(String date) {
        StringTokenizer st = new StringTokenizer(date, "-");
        year = Integer.parseInt(st.nextToken());
        monthNo = Integer.parseInt(st.nextToken()) - 1;
        day = Integer.parseInt(st.nextToken());
        date = year + "-" + monthNo + "-" + day;        
    }    

public String getPreviousMonth(boolean maxDate) {
            Calendar calendar = Calendar.getInstance();

            if (maxDate) {
                calendar.set(year, monthNo, 1);
                int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
                 calendar.set(year, monthNo, maxD);
            } else {
                calendar.set(year, monthNo, day);
            }

            if (monthNo == 0) {
                calendar.add(calendar.MONTH, -1);
            } else {
                calendar.add(calendar.MONTH, -1);
            }
            String date = (calendar.get(calendar.YEAR)) + "-" + (calendar.get(calendar.MONTH) + 1) + "-" + calendar.get(calendar.DAY_OF_MONTH);
            calendar.clear();
            return date;
        }

        public String getNextMonth(boolean maxDate) {
            Calendar calendar = Calendar.getInstance();

            if (maxDate) {
                calendar.set(year, monthNo, 1);
                int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
                calendar.set(year, monthNo, maxD);
            } else {
                calendar.set(year, monthNo, day);
            }
            if (monthNo == 11) {
                calendar.add(calendar.MONTH, 1);
            } else {
                calendar.add(calendar.MONTH, 1);
            }




    public String getCurrentMonth(boolean maxDate){
            Calendar calendar = Calendar.getInstance();
            if (maxDate) {
                calendar.set(year, monthNo, 1);
                int maxD = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
                calendar.set(year, monthNo, maxD);
            } else {
                calendar.set(year, monthNo, day);
            }
            String date = (calendar.get(calendar.YEAR)) + "-" + (calendar.get(calendar.MONTH) + 1) + "-" + calendar.get(calendar.DAY_OF_MONTH);
            calendar.clear();
            return date;
        }

When I set the date to January and February, it gives me the correct output but if I select another month other than January and February I can't get the correct day, followings are some results,

this is OK
2012-1-31 - current month
2011-12-31 - previous month
2012-2-29 - next month

2012-2-29 current month
2012-1-29 previous month ***day should be 31
2012-3-29 next month ***day should be 31

this is OK
2011-12-31 current month
2011-11-30 previous month
2012-1-31 next month

2011-11-30 current month
2011-10-30 previous month *** this should be 31
2011-12-30 next month *** this should be 31

this is OK
2011-12-31 current month
2011-11-30 previous month
2012-1-31 next month

Please tell me where am I wrong...


Solution

  • The problem was that you retrieved maximum amount of days in current month BEFORE actually changing the month.
    Here how should it look.

    public String getNextMonth(boolean maxDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, monthNo, 1);
        calendar.add(Calendar.MONTH, 1);
        if (maxDate) {
            int maxD = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            calendar.set(Calendar.DAY_OF_MONTH, maxD);
        } else {
            calendar.set(Calendar.DAY_OF_MONTH, day);
        }
        String date = (calendar.get(Calendar.YEAR)) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
        calendar.clear();
        return date;
    }