• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding a day to a java.util.date

 
Giselle Dazzi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 java.util.date. I dont know how to turn it into a GregorianCalendar.
Can anyone help me ? either by turning a java.util.date into a Calendar object or by adding / subtracting days to a java.util.date
thx in advance
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recall pain working with these two classes. Brute force solution:
 
Giselle Dazzi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but ... those methods are deprecated...
 
Chris De Vries
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Chris
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giselle:
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic