如何检查日期是否属于当前周?

时间:2014-05-29 09:37:16

标签: java android date

我将首选项中的日期和时间存储为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));

提取日期后,如何检查它是否属于当前周(从星期一开始)?

4 个答案:

答案 0 :(得分:5)

您可以使用Calendar实例根据当前日期创建此星期一和下周一Date个实例,然后检查您的日期是否介于它们之间。

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);

答案 1 :(得分:2)

您可以构建日历,并使用getField(int)同时获取Calendar.YEAR和Calendar.WEEK_OF_YEAR。当它们相同时,两个日期是同一周。

构建日历时,也请调用setFirstDayOfWeek(Calendar.MONDAY)以确保周数从星期一开始(如果不是,则取决于当前的区域设置)。


在@ SiKelly的反对意见之后,这在12月底(1月初)无法可靠地运作:

  1. 比较WEEK_OF_YEAR。
  2. 如果不相等,则为不同的一周。
  3. 如果相等,那可能是错误的一年,但这很难检查。所以还要检查getTime()的差异是否小于8 * 24 * 60 * 60 * 1000(安全方面为8天)。
  4. (你可以先做第3步。然后你可以避免在很多情况下完全构建日历。)

答案 2 :(得分:1)

您可以使用Calendar来计算今天和您的日期的周数,然后再简单地尝试匹配它,

// 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
          }

答案 3 :(得分:0)

试试这个

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;
    }