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