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 7888977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:58:38+00:00 2026-06-03T05:58:38+00:00

I am having the following code Calendar calendar = Calendar.getInstance(); calendar.set(2011, 7, 29); //case

  • 0

I am having the following code

    Calendar calendar = Calendar.getInstance();

    calendar.set(2011, 7, 29);      //case 1    
   // calendar.set(2011, 7, 30);      //case 2
   // calendar.set(2011, 7, 31);    //case 3

    System.out.println("===After seting the date== "+calendar.getTime());

    System.out.println("=================================================================");

    calendar.add(Calendar.MONTH, 6);


    System.out.println("===result after adding 6 months==  "+calendar.getTime());

and for the case 2 and case 3 also i am getting the same results. it should overflow to next month and show the new date. but its not happening.

  • 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-06-03T05:58:39+00:00Added an answer on June 3, 2026 at 5:58 am

    It’s not clear whether you’re using "should" to mean "this is what I want it to do" or to mean "this is what I believe it’s documented to do". The documentation actually backs up the behaviour given:

    add(f, delta) adds delta to field f. This is equivalent to calling set(f, get(f) + delta) with two adjustments:

    Add rule 1. The value of field f after the call minus the value of field f before the call is delta, modulo any overflow that has occurred in field f. Overflow occurs when a field value exceeds its range and, as a result, the next larger field is incremented or decremented and the field value is adjusted back into its range.

    Add rule 2. If a smaller field is expected to be invariant, but it is impossible for it to be equal to its prior value because of changes in its minimum or maximum after field f is changed or other constraints, such as time zone offset changes, then its value is adjusted to be as close as possible to its expected value. A smaller field represents a smaller unit of time. HOUR is a smaller field than DAY_OF_MONTH. No adjustment is made to smaller fields that are not expected to be invariant. The calendar system determines what fields are expected to be invariant.

    In addition, unlike set(), add() forces an immediate recomputation of the calendar’s milliseconds and all fields.

    Example: Consider a GregorianCalendar originally set to August 31, 1999. Calling add(Calendar.MONTH, 13) sets the calendar to September 30, 2000. Add rule 1 sets the MONTH field to September, since adding 13 months to August gives September of the next year. Since DAY_OF_MONTH cannot be 31 in September in a GregorianCalendar, add rule 2 sets the DAY_OF_MONTH to 30, the closest possible value. Although it is a smaller field, DAY_OF_WEEK is not adjusted by rule 2, since it is expected to change when the month changes in a GregorianCalendar.

    The second part of the example is exactly the situation you’re facing: the DAY_OF_MONTH is expected to be invariant, but has to change in order to stay within the right month, so it’s adjusted to the closest possible value (29 in your case).

    So it looks like the behaviour is consistent to me – in what way do you believe it’s inconsistent?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code to get different parts of current system Date (10-11-2011 for
Having the following code: #include <iostream> #include <set> #include <string> #include <functional> using namespace
I am having following code but unable to understand as to why no match
I m having the following code, however, I can see that the radio button
I am having following peice of code ,where in i am trying to serialize
I'm having problems the following code gives me no results. however if I uncomment
Having sorted the tag_counts hash via the following code: sorted_tags = Contact.tag_counts.sort{ |x,y| x.name.downcase
I'm having troubles with the following code: <link type=text/css href=http://jqueryui.com/latest/themes/base/jquery.ui.all.css rel=stylesheet /> <script type=text/javascript
I'm having an ArrayIndexOutofBounds exception with the following code. The exception is thrown at
I'm having a problem with the following code snippet: <!DOCTYPE html> <html> <head> <title>Hangman</title>

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.