• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Calendar - add  RSS feed

 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using the following code:



The output is following:


I was wanting to have output as

which should be in-line with JavaDoc of Calendar:

Usage model. To motivate the behavior of add() and roll(), consider a user interface component with increment and decrement buttons for the month, day, and year, and an underlying GregorianCalendar. If the interface reads January 31, 1999 and the user presses the month increment button, what should it read? If the underlying implementation uses set(), it might read March 3, 1999. A better result would be February 28, 1999. Furthermore, if the user presses the month increment button again, it should read March 31, 1999, not March 28, 1999. By saving the original date and using either add() or roll(), depending on whether larger fields should be affected, the user interface can behave as most users will intuitively expect.


Can anybody please explain the reason for the behaviour and probably the way to get the desired result too.

Regards
Sandeep Jindal


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandeep Jindal wrote:
which should be in-line with JavaDoc of Calendar:

Usage model. To motivate the behavior of add() and roll(), consider a user interface component with increment and decrement buttons for the month, day, and year, and an underlying GregorianCalendar. If the interface reads January 31, 1999 and the user presses the month increment button, what should it read? If the underlying implementation uses set(), it might read March 3, 1999. A better result would be February 28, 1999. Furthermore, if the user presses the month increment button again, it should read March 31, 1999, not March 28, 1999. By saving the original date and using either add() or roll(), depending on whether larger fields should be affected, the user interface can behave as most users will intuitively expect.


Can anybody please explain the reason for the behaviour and probably the way to get the desired result too.


You kinda ignored the explanation the preceded the quote you posted. The section that explained that for certain fields it will adjust for overflows in order to give you the behavior that you mentioned. In the example, in your quoted text, it overflowed. In your example, it did not.

Anyway, if you want to set to the last day of the month, take a look at the getActualMaximum() and the set() methods.

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, ofcourse, one month from 28 February is 28 March, so it shouldn't be a surprise that you get 28 March if you add one month.

First, think about what exactly the requirement is. If you have a date X, then what exactly does your method need to produce? The last day of the month that follows the month that X is in? You need to have a clear and detailed description of what you need exactly before you can write any code.

Example:
 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.

I got my answer/clarification. The behavior if calendar is not same as of my requirement. I need to write the code, similar to written by Jesper.

Thanks again.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!