I see so many posts around so many forums where people want help on adding or subtracting days from a specific date. So I thought Id leave on the wild world of the net some help which might come handy to someone, someday. Here it is.. you can either follow the logic and add some jazz to it and become the hero of your project or do what most of us happily do, copy paste !
import java.util.Calendar;
import java.util.Date;
public class DateAdder {
public Date addOrSubstractDaysFromDate(Date aDate,int noOfDays) {
Calendar calendar = Calendar.getInstance(); //get teh calendar instance
calendar.setTime(aDate);//set it to today
calendar.add(Calendar.DATE, noOfDays);// this is where the adding || subtracting happens
return calendar.getTime();
}public static void main(String[] args){
Date today = new Date();
DateAdder dateAdd = new DateAdder();
System.out.println(“Date today : “+today);
System.out.println(“Date after 10 days : “+ dateAdd.addOrSubstractDaysFromDate(today, 10));
System.out.println(“Date before 10 days : “+dateAdd.addOrSubstractDaysFromDate(today, -10));
}
}
enjoy madi
P.S: the formatting is as usual screwy, or may be I dont know how to do it. This is the best I can do to help. If you need further assistance leave a message and Ill see what I can do for ya !!
July 13, 2008 at 4:25 pm |
thanks maga