File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Adding a day to a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Adding a day to a" Watch "Adding a day to a" New topic

Adding a day to a

Giselle Dazzi
Ranch Hand

Joined: Apr 20, 2003
Posts: 168
Hi all,
how do I add dates in Java ? I guess I could do it via a GregorianCalendar object, but all I have is a I dont know how to turn it into a GregorianCalendar.
Can anyone help me ? either by turning a into a Calendar object or by adding / subtracting days to a
thx in advance

Giselle Dazzi<br />SCJP 1.4
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

I recall pain working with these two classes. Brute force solution:

[How To Ask Questions On JavaRanch]
Giselle Dazzi
Ranch Hand

Joined: Apr 20, 2003
Posts: 168
Thanks, but ... those methods are deprecated...
Chris De Vries
Ranch Hand

Joined: Dec 05, 2002
Posts: 65
How about something like this:

If you run that program, you should get April 13, 2003 and whatever the current time is as output. If you start with a Date object you can use the method Calendar.setTime(Date) method to set the Calendar object to the date represented by the Date object.
I hope this helps.
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
create an caledar object Calendar.getInstance(), and call its setTime(date) method, then calculate it however you want. That should resolve your problem.
Giselle Dazzi
Ranch Hand

Joined: Apr 20, 2003
Posts: 168
Thanks everyone !
I agree. Here's the link:
subject: Adding a day to a
It's not a secret anymore!