1

DateUtil.addDays(days, date) is there any addDays method in java to add days to a date? Provide me any solution for this that how to add days to date

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3054366
  • 51
  • 1
  • 2
  • 3
    How exactly did this extremely poor question got 5 upvotes, even when originally mistagged with [java-ee] tag? – BalusC Dec 02 '13 at 03:24

3 Answers3

2

JDK native date/time handling is pretty poor. The Joda Time library's DateTime is better than java.util.Date class for anything requiring manipulation:

DateTime dt = new DateTime();
DateTime twoDaysLater = dt.plusDays(2);

If you want to create a DateTime from a java.util.Date:

Date nativeDate = new Date();
DateTime dt = new DateTime(nativeDate);

If you need to convert back to a java.util.Date:

Date date = dt.toDate();
joews
  • 29,767
  • 10
  • 79
  • 91
2

You can use calendar.

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 5); // Adding 5 days
String output = sdf.format(c.getTime());
System.out.println(output);
dev
  • 715
  • 5
  • 21
2

The closest thing in the JDK is using TimeUnit:

date.setTime(date.getTime() + TimeUnit.DAYS.toMillis(days));

It is interesting to note that (quite unbelievably) Date is not immutable! Just one of many questionable design decisions in the JDK.

Community
  • 1
  • 1
Bohemian
  • 412,405
  • 93
  • 575
  • 722