aspose file tools
The moose likes Beginning Java and the fly likes Incrementing Date By 6 Months Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Incrementing Date By 6 Months" Watch "Incrementing Date By 6 Months" New topic
Author

Incrementing Date By 6 Months

Rajesh Rajaram
Ranch Hand

Joined: Aug 20, 2002
Posts: 63
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,


Rajesh Rajaram
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Have a look at the Calendar class


Joanne
Christian
Greenhorn

Joined: Sep 16, 2008
Posts: 1
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

Joined: Oct 27, 2005
Posts: 19214

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Mdoc Doctor
Greenhorn

Joined: Sep 19, 2008
Posts: 1
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
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
"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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: Incrementing Date By 6 Months
 
Similar Threads
recent movie
Current date - 10
When the new book for certification 1.5 will be lauched?
getting first and last date of past 6 months?
Problem with adding months to Gregorian Calender