I have a database that contains news items, among other things. When these news items are created and added to the database, the date they were created is recorded as well in medium format. Ex. 18-June-2006
My problem is that, I have written a servlet to retrieve news items and publish them on my homepage, however I need these news items to be removed after a certain life time. For example, to expire after 2 weeks.
How can I go about doing something like this? I know I could retrieve the current date and the creation date, but how can I find out how many days have passed between the two? I'd greatly appreciate any help with this!!
Class java.util.Calendar contains methods to do date arithmetic. You can for example use the method add(...) to add or subtract a number of hours, days, months etc. from a date, and you can use the methods before(...) or after(...) to find out if a date is before or after another date.
With the combination of those methods, you can solve the problem.
You could also do this in SQL; if you really want to delete items older than two weeks, run an SQL statement like "delete from newsitems where timestamp ..." (you'll have to find out the exact SQL yourself; read the SQL reference documentation of your database). [ June 19, 2006: Message edited by: Jesper Young ]