I have a Database and i am saving logs as well for the each update performed by users on Database. I want to check if user is updating Data within 24 hours then update the Log, else if it's more than a day since data is updated then instead of updating the log for the day insert new log. for that i need to set upper bound and lower bound for the time on which data is inserted.
I have write down a function that will check if data is updated since 24 hours or not, but i dont know what to put as upper bound and lower bound for a day.
// returns 24 hours records, by Id and Crop...
private FarmerCropDataLog findLogByIdAndCrop(Farmer farmer, Crop cropData) {
String farmerId = farmer.getId();
// TODO Auto-generated method stub
Query query = new Query();
query.addCriteria(Criteria.where(FarmerCropDataLog.Constants.FARMER_ID).is(farmerId));
query.addCriteria(Criteria.where(FarmerCropDataLog.Constants.CROP).is(Crop.valueOf(Crop.class, cropData.name())));
query.addCriteria(Criteria.where(FarmerCropDataLog.Constants.CREATION_TIME).lt(UPPER_BOUND));
query.addCriteria(Criteria.where(FarmerCropDataLog.Constants.CREATION_TIME).gte(LOWERBOUND));
List<FarmerCropDataLog> logData = farmerCropDataLogDao.runQuery(query, FarmerCropDataLog.class);
if (logData != null) {
return logData.get(0);
} else {
return null;
}
}
Edit ->
Upper Bound should be 24*60*60*1000 and lowerBound should be 0?
Based on the info in the comments, I think you can valorize bounds values as follows:
long millisPerDay = 24 * 60 * 60 * 1000;
long upperBound = System.currentTimeMillis();
long lowerBound = upperBound - millisPerDay;
CAUTION: you should check whether you timestamp field uses Unix Timestamps (in seconds) or millis since 01/01/1970: in the first scenario, your values should be divided by 1000.
I hope this can help you tu setup your query properly...
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments