Monday, August 04, 2008

How to add days (Say 10 or 30 days) to a existing date (Say current date) and return the new date?

/*
How to add days (Say 10 or 30 days) to a existing date (Say current date) and return the new date?
Usage: To find the expiry date of user id. Example: Whenever user logins to a system, his lastlogondate is updated with current date.If in a system user does not login for 90 days, then the user id should be locked. For suchkind of checking we can use this logic
*/

import java.util.Date;
public class DateTest1 {

/*
If the new date send by addCalcDate is after todays current date then we disable the user from the system.
*/

public static void main(String args[]) {
if(todaysSqlDate().after(addCalcDate(objGTEMUser.getLastLogonDate(),90))) {
System.out.println("Disable the user: ");
}
}

/*
We pass the last logon date and add 90 days to it and send the new date.
*/

public static Date addCalcDate(Date baseDate,int calc) {
java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
cal.setTime(baseDate);
cal.add(java.util.GregorianCalendar.DATE, calc);
return cal.getTime();
}


public static java.sql.Date todaysSqlDate(){
return new java.sql.Date(Calendar.getInstance().getTimeInMillis());
}
}

0 Comments:

Post a Comment

<< Home