0 votes
1 view
in Java by (3.6k points)

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.

1 Answer

0 votes
by (35.8k points)

Given a Date dt you have many possibilities:

1: You can try the Calendar class for that:

Date dt = new Date();

Calendar c = Calendar.getInstance(); 

c.setTime(dt); 

c.add(Calendar.DATE, 1);

dt = c.getTime();

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

Date dt = new Date();

DateTime dtOrg = new DateTime(dt);

DateTime dtPlusOne = dtOrg.plusDays(1);

 3: With Java 8 you can also try the new JSR 310 API (which is inspired by Joda-Time):

Date dt = new Date();

LocalDateTime.from(dt.toInstant()).plusDays(1);

...