Android : Check if timestamp (milli) belongs to a specific day

on Saturday, July 5, 2014


In a table of my SQLite database there is a timestamp (millisec) column. This table is really huge and I want a efficent way of querying all the rows that theire timestamp column refers to a specific day (in this case I want to get all enteries that belong to TODAY).


I came up with this, but it does not work and its quite slow. Any ideas will be appriciated. (NO I can not make any change in the database such as adding a DATETIME column...I have to do it with the timestamp).



public boolean isTimestampInToday(long timestamp){

//get a timestamp from today
long todayStamp = System.currentTimeMillis();

Calendar c = Calendar.getInstance();
c.setTimeInMillis(todayStamp);
c.setTimeZone(TimeZone.getDefault());

c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);

Date startDate = c.getTime(); //START OF DAY

c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);

Date endDate = c.getTime(); //END OF DAY

Long startStamp = startDate.getTime();
Long endStamp = endDate.getTime();

if(timestamp >= startStamp && timestamp < endStamp) {
return true;
} else {
return false;
}
}

0 comments:

Post a Comment