如何确定是星期几还是最后一天? [英] how to determine whether the date of this week or last?

查看:178
本文介绍了如何确定是星期几还是最后一天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有日期数组。例如10个日期。我需要计算每个日期-是本周还是上周。

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> ,如果收据有本周的日期,则将其添加到新列表中。

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

推荐答案

使用类似以下内容进行比较:

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

为了获得更正确的结果,您也可以检查年份(而不仅仅是一年中的星期),您可以这样操作: int year = c.get(Calendar.YEAR);
http://developer.android.com /reference/java/util/Calendar.html#WEEK_OF_YEAR

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

这篇关于如何确定是星期几还是最后一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆