1

I have date array. For example 10 dates. I need calculate each date - it is this week or last week. How do make this?

private List<Receipt> getSectionPosition(List<Receipt> receiptList){
       List<Receipt> list = new ArrayList<>();
       Date now = new Date();
        for (int i = 0; i<receiptList.size(); i++){
            if (receiptList.get(i).getCreatedDate().compareTo(now)==0){
                list.add(receiptList.get(i));
            }
        }
       return list;
    }

i pass List<Receipt> and if receipt has date on this week i add it in new list. but it is not work.

ip696
  • 6,574
  • 12
  • 65
  • 128

2 Answers2

2

Use something like this for comparing:

private List<Receipt> getSectionPosition(List<Receipt> receiptList){
   List<Receipt> list = new ArrayList<>();
   Date now = new Date();
   Calendar c = Calendar.getInstance();
   c.setTime(now);
   int currentWeek = c.get(Calendar.WEEK_OF_YEAR);
   for (int i = 0; i<receiptList.size(); i++){
       c.setTime(receiptList.get(i).getCreatedDate());
       int createdDateWeek = c.get(Calendar.WEEK_OF_YEAR);

       if (currentWeek == createdDateWeek){
           list.add(receiptList.get(i));
       }
   }
   return list;
}

P.S. for more correct results, you could check the year, too (not just the week of the year), which you could do like this: int year = c.get(Calendar.YEAR); http://developer.android.com/reference/java/util/Calendar.html#WEEK_OF_YEAR

Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • it work! even a small question. I need to change, to add just last and this week? – ip696 Jun 16 '15 at 09:31
  • Sorry, I don't understand your question.. could you ask it again? You need to check if the date is in the previous week or? – Gabriella Angelova Jun 16 '15 at 09:48
  • Aaaa you want to check if the date is in currentWeek or in last week? If yes - you could change this prove statement `if (currentWeek == createdDateWeek){` to this one `if (currentWeek == createdDateWeek || createdDateWeek == currentWeek - 1){`because the currentWeek is actually the number of the week in the year and when you get the `currentWeek - 1` you become the previous week – Gabriella Angelova Jun 16 '15 at 09:50
  • What about boundary conditions? e.g. if current week is first week of year? – Dev Jul 29 '15 at 05:58
0

Create a method compare dates

public boolean isTheSameWeek (Date date ){

Calendar c=Calendar.getInstance();
year1=c.get(c.YEAR);
week1=c.get(c.WEEK_OF_YEAR);

Calendar c1=Calendar.getInstance();
c.setTimeInMillis(date.getTime())
year2=c.get(c.YEAR);
week2=c.get(c.WEEK_OF_YEAR);

if(year1 == year2){
       if(week1 == week2}{
       return true;

       }
    }
return false
}}

In your method call this

     if (isTheSameWeek(receiptList.get(i).getCreatedDate())){
Raluca Lucaci
  • 2,058
  • 3
  • 20
  • 37
  • 1
    It is not right to use `c.get(c.YEAR)`, it should be `c.get(Calendar.YEAR)` http://stackoverflow.com/questions/10161637/getting-current-year-and-month-resulting-strange-results – Gabriella Angelova Jun 16 '15 at 09:10