PugsOverDrugs PugsOverDrugs - 1 year ago 100
Java Question

How do I add 2 weeks to a Date in java?

I am getting a Date from the object at the point of instantiation, and for the sake of outputting I need to add 2 weeks to that date. I am wondering how I would go about adding to it and also whether or not my syntax is correct currently.

Current Java:

private final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
private Date dateOfOrder;

private void setDateOfOrder()
//Get current date time with Date()
dateOfOrder = new Date();

public Date getDateOfOrder()
return dateOfOrder;

Is this syntax correct? Also, I want to make a getter that returns an estimated shipping date, which is 14 days after the date of order, I'm not sure how to add and subtract from the current date.

Answer Source

Use Calendar and set the current time then user the add method of the calendar

try this:

int noOfDays = 14; //i.e two weeks
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, noOfDays);
Date date = calendar.getTime();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download