0

I have the date and time stored in preferences as Long:

// put
SharedPreferences settings =
    PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
settings.edit().putLong("pref_datetime", System.currentTimeMillis()).commit();
// get
Date date2 = new Date(settings.getLong("pref_datetime", 0));

Once date is extracted, how could I check if it belongs to the current week (starting on Monday) or not?

LA_
  • 19,823
  • 58
  • 172
  • 308

4 Answers4

5

You can use a Calendar instance to create this Monday and next Monday Date instances based on the current date then check that your date lies between them.

Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.MONDAY);

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

Date monday = c.getTime();

Date nextMonday= new Date(monday.getTime()+7*24*60*60*1000);

boolean isThisWeek = date2.after(monday) && date2.before(nextMonday);
Si Kelly
  • 703
  • 3
  • 9
2

You can construct a Calendar, and use getField(int) to get both the Calendar.YEAR and the Calendar.WEEK_OF_YEAR. When those are the same, the two Dates are from the same week.

When constructing the Calendar, also call setFirstDayOfWeek(Calendar.MONDAY) to make sure the weeks start on Monday (if you don't, it depends on the current locale).


After @SiKelly's objection that this won't work reliably in the end of December, beginning of January:

  1. Compare WEEK_OF_YEAR.
  2. If not equal, it is a different week.
  3. If equal, it could be the wrong year, but that is hard to check. So just also check that the difference in getTime() is less than 8 * 24 * 60 * 60 * 1000 (eight days to be on the safe side).

(you could do step 3 first. then you avoid having to construct the calendar at all in many cases).

Thilo
  • 257,207
  • 101
  • 511
  • 656
1

You can use Calendar to calculate week number for today and your date also,then simpy try to match it,

// put
SharedPreferences settings =
    PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
settings.edit().putLong("pref_datetime", System.currentTimeMillis()).commit();
// get
Date date2 = new Date(settings.getLong("pref_datetime", 0));              

          Calendar calendar = Calendar.getInstance();
          calendar.setFirstDayOfWeek(Calendar.MONDAY);
          int current_year = calendar.get(Calendar.YEAR);
          int current_week_number = calendar.get(Calendar.WEEK_OF_YEAR);
          calendar.setTime(date2);
          int year = calendar.get(Calendar.YEAR);
          int week_number= calendar.get(Calendar.WEEK_OF_YEAR);
          if(current_year==year && current_week_number==week_number)
          {
              //same
          }
          else
          {
              //different
          }
Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
0

try this

private boolean  is_ThisWeek(Date date2){

        Date now =new Date(System.currentTimeMillis());
        int diffInDays = (int)( (now.getTime() - date2.getTime()) 
                 / (1000 * 60 * 60 * 24) );

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        int reslut = calendar.get(Calendar.DAY_OF_WEEK);
        int days_before = get_days_before(reslut);

        if (diffInDays<days_before) {
            Toast.makeText(this, "this week", Toast.LENGTH_SHORT).show();
            return true;
        }else {
            Toast.makeText(this,"not this week", Toast.LENGTH_SHORT).show();
            return false;
        }
     }
    int get_days_before(int reslut){
        int days_before = 0;
        switch (reslut) {
        case Calendar.MONDAY:
            days_before=1;
            break;
        case Calendar.TUESDAY:
            days_before=2;
            break;
        case Calendar.WEDNESDAY:
            days_before=3;
            break;
        case Calendar.THURSDAY:
            days_before=4;
            break;
        case Calendar.FRIDAY:
            days_before=5;
            break;
        case Calendar.SATURDAY:
            days_before=6;
            break;
        case Calendar.SUNDAY:
            days_before=7;
            break;
        }
        return days_before;
    }
Shijil
  • 2,226
  • 18
  • 33