Retrive items from database where DATE format is older than x hours or sec

268 Views Asked by At

I have a Cronjob that needs to take some items from DB by a condition clause. In that condition, I need to compare some Date's time and they are like (Fri Dec 18 12:36:07 EET 2020)(java.util.Date) in the database. When the query is executed, I need to retrieve from the database the items that are older than 1 hour for example by the current time.
I think that my query needs to be like
select * from table_name where date_column > int_variable.
The thing is the variable is an integer that is set from elsewhere. How can I do this thing? Do I need to change the variable from int to something else? I let my class below as an example. In my class, I don't have yet the example query I write above. Thanks a lot!

public class HttpSessionCleanJob extends AbstractJobPerformable<HttpSessionCleanJobModel> {

private static final Logger LOG = Logger.getLogger(HttpSessionCleanJob.class.getName());

@Resource
private ModelService modelService;
@Resource
private FlexibleSearchService flexibleSearchService;

@Override
public PerformResult perform(final HttpSessionCleanJobModel httpSessionCleanJobModel) {

    LOG.info("Start cleaning StoredHttpSession items...");
    final int maxJobTime = httpSessionCleanJobModel.getTime();

    final String queryString = "SELECT {" + StoredHttpSessionModel.PK + "} FROM {" + StoredHttpSessionModel._TYPECODE + "}";

    final Date specificTime = new Date();
    final FlexibleSearchQuery query = new FlexibleSearchQuery(queryString, Collections.singletonMap("specificTime", specificTime));

    final SearchResult<StoredHttpSessionModel> searchResult = flexibleSearchService.search(query);

    List<StoredHttpSessionModel> assetModelList = searchResult.getResult();

    for(StoredHttpSessionModel storedHttpSessionModel : assetModelList) {
        LOG.info(storedHttpSessionModel.getModifiedtime());
    }

    LOG.info("Finished cleaning StoredHttpSession items...");
    return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
}
}
2

There are 2 best solutions below

2
On

You can try something like this :

specificTime.setTime(specificTime.getTime() - 5000)

The result is the current date minus 5000 milliseconds (5 seconds).

Then :

 final String queryString = "SELECT {" + StoredHttpSessionModel.PK + "} FROM {" + StoredHttpSessionModel._TYPECODE + "} WHERE {modifiedTime} < ?specificTime";

 final Date specificTime = new Date();
 final FlexibleSearchQuery query = new FlexibleSearchQuery(queryString, Collections.singletonMap("specificTime", specificTime));

This will retreive all StoredHttpSessionModel whose modification date has exceeded 5 seconds,

Adapt the code to correspond more to your needs.

0
On

I solved this with:

final String queryString = "SELECT {" + StoredHttpSessionModel.PK + "} FROM {" + StoredHttpSessionModel._TYPECODE + "} WHERE {" + StoredHttpSessionModel.MODIFIEDTIME +"} < now() - interval ?maxJobTime SECOND";