Mani Deep
Mani Deep

Reputation: 1356

Add days to current date

Here is my code

 public java.util.Date getDelDate() {
    Date m=new Date();
    System.out.println("ticket date "+ m);
    double d;
    d=(multiply/consumption);
    int days= (int) d;
    System.out.println("del days "+ days);
    m.setTime(m.getTime() + days * 1000 * 60 * 60 * 24);
    System.out.println(m+"Delivery date");
    return m;
}
public java.util.Date getRemDate() {
    Date m1=new Date();
    double d;
    d=(multiply/consumption);
    int days= (int) d-2;
    System.out.println("rem days "+ days);
    m1.setTime(m1.getTime() + days * 1000 * 60 * 60 * 24);
    System.out.println(m1+"Remember date");
    return m1;
    //return remdate;
}

for Input values of multiply = 21 and consumption = 2 the output is (correct as follows)

ticket date Wed Oct 02 21:43:56 IST 2013
del days 10
Sat Oct 12 21:43:56 IST 2013Delivery date
rem days 8

for Input values of multiply = 35 and consumption = 1 the output is (wrong as follows showing old date)

ticket date Wed Oct 02 21:52:07 IST 2013
del days 35
Wed Sep 18 04:49:20 IST 2013Delivery date
rem days 33
Mon Sep 16 04:49:20 IST 2013Remember date

for Input values of multiply = 1 and consumption = 0.03 the output is (wrong as follows showing old date)

ticket date Wed Oct 02 21:26:56 IST 2013
del days 33
Mon Sep 16 04:24:09 IST 2013Delivery date   //date here is sept?
rem days 31
Sat Sep 14 04:24:09 IST 2013Remember date

how do i calculate the correct date?

Upvotes: 2

Views: 5356

Answers (2)

David
David

Reputation: 1296

I suggest you use a Calendar. An implementation is part of java.

To add days or to subtract days you use the same method add():

public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    int days = 35;
    // Add days
    calendar.add(Calendar.DAY_OF_MONTH, days);
    System.out.println(calendar.getTime());
}

If you want to subtract days, just use a negative number of days.

Upvotes: 4

Cedric Simon
Cedric Simon

Reputation: 4659

You are behind the max value of an int so you get a negative value :S

Change the int days for long days

Y listo!

Upvotes: 1

Related Questions