Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7441601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:02:32+00:00 2026-05-29T11:02:32+00:00

I have a java programe which I can set a date in yyyy-MM-dd and

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:02:33+00:00Added an answer on May 29, 2026 at 11:02 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a Perl program to validate XML which is invoked from a Java
I have a Java program in which an action is initiated which loops through
I currently have a small Java program which I would like to run both
I now have a running Java program which only lacks of the final step,that
How to write a java program which will tell me whether I have internet
I have a program which i have myself written in java, but I want
I have two strings in a java program, which I want to mix in
We have a Java program run as root on Unix, that therefore can read
I have a Java application which needs to be audited (so obviously I need
I have a stored proc in SQL Server 2005, which looks like the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.