Question: I am trying to delete a row in my table. Here is my sample code

```
String DBDate = new String();
Long date = new Date().getTime();           
String dateToday = date.toString();             
dbobj.clear();
dbobj.put(LAST_TIME_ANSWERED, "");
int hasDB = dbobj.getFromDB(tableName,column key, primary key);

if(hasDB != 0) {
    DBDate = dbobj.get(LAST_TIME_ANSWERED);
    if(DBDate != dateToday) {
        dbobj.deleteClientDB(tableName, column key, primary key);
    }
}

```

How could I change the date of a timer?
Any help would be nice. Thanks in advance!

Answer: 
One common practice is to delete database records which are older than some threshold value. For this purpose, the database table should have a column with a timestamp representing insert or update time. When executing the scheduled task there a command is performed (pseudocode, assuming an 'update' timestamp):

DELETE FROM TABLE\_NAME WHERE LAST\_UPDATED < (CURRENT\_TIME - THRESHOLD)

Or there is a select query performed to retrieve all these records and handle their deletion one by one including logging and maybe some conditional logic.

To achieve this functionality java SE provides [ScheduledExecutorService](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledExecutorService.html) which can be used to execute delayed tasks:

```
    // initialize scheduler
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    // schedule repeating task
    ScheduledFuture future = scheduler.scheduleAtFixedRate(
            () -> {
                System.out.println("Excuting task...");
            },
            60,
            60,
            TimeUnit.SECONDS
    );

```

The future object can be used to cancel task:

```
future.cancel(true);
scheduler.shutdown();

```

But when using enterprise solutions, there are normally libraries and frameworks, providing functionality for executing scheduled tasks: [quartz scheduler](https://github.com/quartz-scheduler/quartz), [spring framework task scheduling](https://spring.io/guides/gs/scheduling-tasks/) or [EJB timer services](https://docs.oracle.com/javaee/6/tutorial/doc/bnboy.html).
there are two options for this.
1.) use java scheduler or
2.) use a scheduled job at DB level to clear data at the end of each day