• Post Reply Bookmark Topic Watch Topic
  • New Topic

date filtering in JAVA  RSS feed

 
bob Ross
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am looking for date filtering :- I have some items with date and i need to put them as "today" "tomorrow" "This week" "Next week" "Latter" "Past Dated" .

what is the best approach.

1) Take out number of days between item date and today

double longdiff = date.getTime() - today.getTime();
double floatdiff = longdiff / (1000.0 * 60 * 60 * 24);


2) It can be + ve - ve if - ve its past date

3)if 0 its today

4) if floatdiff < 1 && floatdiff > 0 then its tomorrow

5) Problem is for "This week" "Next week" "Latter"


Calendar calendar = Calendar.getInstance();

Calendar calendar2 = Calendar.getInstance();

int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);

calendar2.setTime(date);

int givenWeek = calendar2.get(Calendar.WEEK_OF_YEAR);

if (currentWeek == givenWeek) {
return THIS_WEEK;
} else if (currentWeek + 1 == givenWeek) {
return NEXT_WEEK;
} else
return LATER;


6) Still The problem will be, when there is year change like 1 st jan 2010 is 1 st week of year and 30 dec 2009 is almost 52 or 53 rd week so above algo will not work as expected ...

Any robust solution approach for this issue please post .....

Thanks
Bob

 
Jarred Olson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use Code Tags when posting code. As far as a solution for your question. You could add logic to take into account your end of the year scenario since you know it will occur only on week 52 (or 53 in rare cases).

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!