• Post Reply Bookmark Topic Watch Topic
  • New Topic

Incrementing Date By 6 Months  RSS feed

 
Rajesh Rajaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In my application I am planning to increment the current date to 6 months.

Let�s say if the user enters today's date 09/10/2008 I want to update

03/10/2009.

Is there any API to do achieve this?


Thanks,
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Calendar class
 
Christian
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following will do it

Code:



Output:
Tue Sep 16 16:36:40 PDT 2008
Mon Mar 16 16:36:40 PDT 2009
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Christian Willi:

Calendar.add is preferred:

Your code will most likely fail if the new month is in the next year. After all, what is month 13? From the API:
Throws:
ArrayIndexOutOfBoundsException - if the specified field is out of range (field < 0 || field >= FIELD_COUNT). in non-lenient mode.

Calendar.add is designed to handle that.
 
Mdoc Doctor
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I would recommend using Joda Time which is an Open Source Java package and it is 10 fold better than the poorly designed Java Date Time API.
http://joda-time.sourceforge.net/

In addition to that a new Java JSR is based on this open source package.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Mdoc Doctor", you appear to have missed the naming policy whihc requires first name-space-last name not obviously fictitious. Your displayed name does not conform; please go to "my profile" "update profile" and correct the displayed name to comply.

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