-2

The date format iam using is "MM/dd/yy" I want to perform a operation like This:

LastActDate = Todaysdate -(PrchaseDate + 1) 

In myDb the date is stored as 2011-07-08 19:30:06(java util date

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
user572598
  • 27
  • 1
  • 1
  • 3
    What's stopping you from performing the operation? What code do you have? What errors are you getting? – S.Lott Oct 20 '11 at 10:15
  • possible duplicate of [How to add days to a date in Java](http://stackoverflow.com/questions/2507377/how-to-add-days-to-a-date-in-java) – Don Roby Oct 20 '11 at 10:22

3 Answers3

2

Use Calendar.add(int field, int amount)

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

import java.util.GregorianCalendar;
import java.util.Calendar;

class AddDates {
    public static void main(String[] args) {

        Calendar today = new GregorianCalendar();
        Calendar purchased = new GregorianCalendar();

        purchased.add(Calendar.DAY_OF_MONTH, 1);

        Calendar lastActDate = new GregorianCalendar();
        lastActDate.setTimeInMillis(today.getTimeInMillis() - purchased.getTimeInMillis());
    }
}
Saul
  • 17,973
  • 8
  • 64
  • 88
1

I highly recommend using joda library for any date/time calculations.

You can do the kind of work you want to do above with it very easily just take a look at examples on the site.

Ashkan Aryan
  • 3,504
  • 4
  • 30
  • 44
0

This should work. Assuming the +1 is "plus one day".

try {
    String dateString = "10/20/11";

    SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy");
    Date dt = df.parse(dateString);

    final long ONE_DAY = 1000 * 60 * 60 * 24;      
    Date lastActDate = new Date(System.currentTimeMillis() - (dt.getTime() + ONE_DAY));

    String lastActDateString = df.format(lastActDate);
}
catch ( ParseException pe ) {
    pe.printStackTrace();
}
Matt MacLean
  • 19,410
  • 7
  • 50
  • 53