17

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.

PugsOverDrugs
  • 515
  • 1
  • 4
  • 14

7 Answers7

41

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.setTime(dateOfOrder);            
calendar.add(Calendar.DAY_OF_YEAR, noOfDays);
Date date = calendar.getTime();
majidarif
  • 18,694
  • 16
  • 88
  • 133
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
16

I will show you how we can do it in Java 8. Here you go:

public class DemoDate {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        System.out.println("Current date: " + today);

        //add 2 week to the current date
        LocalDate next2Week = today.plus(2, ChronoUnit.WEEKS);
        System.out.println("Next week: " + next2Week);
    }
}

The output:

Current date: 2016-08-15
Next week: 2016-08-29

Java 8 rocks !!

PAA
  • 1
  • 46
  • 174
  • 282
8

Use Calendar

    Date date = ...
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(Calendar.WEEK_OF_MONTH, 2);
    date = c.getTime();
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
4

Try this to add two weeks.

long date = System.currentTimeMillis() + 14 * 24 * 3600 * 1000;
Date newDate = new Date(date);
spongebob
  • 8,370
  • 15
  • 50
  • 83
Blue Ocean
  • 282
  • 1
  • 10
0

if pass 14 to this addDate method it will add 14 to the current date and return

public String addDate(int days) throws Exception {
    final DateFormat dateFormat1 = new SimpleDateFormat(
            "yyyy/MM/dd HH:mm:ss");
    Calendar c = Calendar.getInstance();
    c.setTime(new Date()); // Now use today date.
    c.add(Calendar.DATE, addDays); // Adding 5 days
    return dateFormat1.format(c.getTime());
}
loknath
  • 1,362
  • 16
  • 25
0

Using the Joda-Time library will be easier and will handle Daylight Saving Time, other anomalies, and time zones.

java.util.Date date = new DateTime( DateTimeZone.forID( "America/Denver" ) ).plusWeeks( 2 ).withTimeAtStartOfDay().toDate();
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

If you are on java 8 you can use new date time api http://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#plusWeeks-long-

if you are on java 7 or more old version of java you should use old api http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#add-int-int-