bvdo
bvdo

Reputation: 81

Changing the date

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.

Upvotes: 0

Views: 75

Answers (2)

Ilyas Soomro
Ilyas Soomro

Reputation: 253

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();
}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

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()));

Upvotes: 1

Related Questions