tags:

views:

599

answers:

5

I want to add one day to a particular date. How can I do that?

Date dt = new Date();

Now I want to add one day to this date.

+25  A: 

Solution 1: You can use the Calendar class for that:

Calendar c = Calendar.getInstance(); 
c.setTime(dt); 
c.add(Calendar.DATE, 1);
dt = c.getTime();

Solution 2: You should seriously consider using the Joda library, because of the various shortcomings of the Date class. With Joda you can do the following:

DateTime dtOrg = new DateTime(dt);
DateTime dtPlusOne = dtOrg.plusDays(1);
DR
+1 for Joda time
belugabob
+4  A: 

In core Java, Calendar is recommended for date manipulation.

Check this out: http://www.java2s.com/Tutorial/Java/0040__Data-Type/0580__Calendar.htm

aberrant80
+1  A: 

To make it a touch less java specific, the basic principle would be to convert to some linear date format, julian days, modified julian days, seconds since some epoch, etc, add your day, and convert back.

The reason for doing this is that you farm out the "get the leap day, leap second, etc right' problem to someone who has, with some luck, not mucked this problem up.

I will caution you that getting these conversion routines right can be difficult. There are an amazing number of different ways that people mess up time, the most recent high profile example was MS's Zune. Dont' poke too much fun at MS though, it's easy to mess up. It doesn't help that there are multiple different time formats, say, TAI vs TT.

Bruce ONeel
+1  A: 
Date today = new Date();
Date tomorrow = new Date(today.getTime() + (1000 * 60 * 60 * 24));

Date has a constructor using the milliseconds since the UNIX-epoch. the getTime()-method gives you that value. So adding the milliseconds for a day, does the trick. If you want to do such manipulations regularly I recommend to define constants for the values.

Mnementh
A: 

use DateTime object obj.Add to add what ever you want day hour and etc. Hope this works:)

Vinay Pandey