94

How do I add x days to a date in Java?

For example, my date is 01/01/2012, using dd/mm/yyyy as the format.

Adding 5 days, the output should be 06/01/2012.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
hari prasad
  • 895
  • 1
  • 6
  • 9
  • 4
    There are more duplicates for this general question http://stackoverflow.com/questions/2623610/subtracting-days-in-a-calendar-object http://stackoverflow.com/questions/428918/how-can-i-increment-a-date-by-one-day-in-java – AurA Aug 23 '12 at 08:22
  • 1
    Search for Joda-Time DateTime plusDays for many examples. – Basil Bourque Dec 26 '13 at 07:58
  • 1
    Given you’re using the effectively deprecated `Date` class, the *simplest* (not the best) way is: `returnDate = new Date(borrowDate.getTime() + TimeUnit.DAYS.toMillis(14));` – Bohemian Jan 11 '19 at 06:42
  • use DateUtils.addDays(date,3); from org.apache.commons.lang.time library. – saba Jan 10 '22 at 06:02

6 Answers6

120
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Using today's date
c.add(Calendar.DATE, 5); // Adding 5 days
String output = sdf.format(c.getTime());
System.out.println(output);
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
swemon
  • 5,840
  • 4
  • 32
  • 54
58

java.time

With the Java 8 Date and Time API you can use the LocalDate class.

LocalDate.now().plusDays(nrOfDays)

See the Oracle Tutorial.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
  • 3
    Correct answer. I would also suggest passing a time zone ([`ZoneId`](http://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html)) to the [`now`](http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#now-java.time.ZoneId-) method rather than relying implicitly on the JVM’s [current default time zone](http://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html#systemDefault--). That default can change at any moment *during runtime*! Better to specify the desired/expected time zone. – Basil Bourque Mar 16 '16 at 22:03
25
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.YEAR, 2012);
cal.add(Calendar.DAY_OF_MONTH, 5);

You can also subtract days like this: Calendar.add(Calendar.DAY_OF_MONTH, -5);

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
23

Here is some simple code that prints the date five days from now:

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 5);
System.out.println(dateFormat.format(c.getTime()));

Example output:

16/12/2021

See also: Calendar#add

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
user3136058
  • 231
  • 2
  • 2
16

If you're using Joda-Time (and there are lots of good reasons to - a simple, intuitive API and thread safety) then you can do this trivially:

new LocalDate().plusDays(5);

to add 5 days to today's date, for example.

EDIT: My current advice would be to now use the Java 8 date/time API

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
13

Simple, without any other API:

To add 8 days to the current day:

Date today = new Date();
long ltime = today.getTime()+8*24*60*60*1000;
Date today8 = new Date(ltime);
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
praagma
  • 147
  • 1
  • 4
  • 8
    This code does not account for the fact that days are not always 24-hours long. Technically, this is adding 192 hours rather than 8 days. May be correct depending on what the situation defines as "days". – Basil Bourque Mar 16 '16 at 22:06
  • 1
    'public static Date addRemoveDays(Date d, int days) { long ltime=d.getTime()+days*24*60*60*1000; return new Date(ltime); } ... Date danes = new Date(); //28.4.2016 Date tooOld = MyUtil.addRemoveDays(danes, -80); //18.5.2016 ERROR???' – MatejC Apr 28 '16 at 16:50
  • 3
    Problem with using code like this is that you can easily overflow int and then go negative: 90 days * 24*60*60*1000 = (int) -813934592 This takes you _backwards_. – Byron Katz Jan 09 '18 at 16:14
  • 2
    Not a way to implement like this, i have used this code when i add 29 + 7 it went for 36 where as in date no more than 31 days...... – Syed Hamza Hassan Oct 30 '18 at 11:26