0

Basically, I've got a little program that uses date.

Date current = new Date();
current.setDate(current.getDay() + time1);

When I do this it adds to the day, but say time1 = 30 then the month doesn't change when I print the date out. I hope this makes sense I'm kinda new to this.

bvdo
  • 81
  • 1
  • 7
  • use .getDate instead of .getDay . The value returned by getDay is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on. – WannaBeGeek Dec 23 '14 at 05:04
  • 3
    A lot of the Date methods are deprecated. Calendar has a roll() function that does what you're looking for, but I highly recommend looking into Joda Time. (http://www.joda.org/joda-time/) – haley Dec 23 '14 at 05:05
  • 1
    You are using a deprecated `Date`method. See http://stackoverflow.com/questions/2901262/why-were-most-java-util-date-methods-deprecated – Raedwald Dec 23 '14 at 07:45
  • possible duplicate of [How to add one day to a date?](http://stackoverflow.com/questions/1005523/how-to-add-one-day-to-a-date) – Basil Bourque Dec 23 '14 at 08:11

2 Answers2

1

Use a Calendar to perform date arithmetic and a DateFormat to display the result. Something like,

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 30);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df.format(cal.getTime()));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Use this method

 public static Date addDaystoGivenDate(Integer days, Date date){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DAY_OF_MONTH, days);
    return cal.getTime();
}
Ilyas Soomro
  • 253
  • 2
  • 8