Android:如何使用Calendar类检查一周是否已经过去

时间:2018-11-12 20:52:11

标签: java android android-calendar

我想检查一周是否已经过去(使用Calendar班级,并且是否打印了一条消息。

int lastCallDate = Calendar.SATURDAY;

    int day = calender.get(Calendar.DAY_OF_WEEK);

    if (day == lastCallDate + 7){
    {
            System.out.println("It's been a week");

        } else {

             System.out.println("It hasn't been a week");




        }

任何建议或答案都很棒!

谢谢。

2 个答案:

答案 0 :(得分:0)

if (day %lastCallDate ==0)
{
        System.out.println("It's been a week");

    } else {

         System.out.println("It hasn't been a week");
    }
}

答案 1 :(得分:0)

首先,您可以将第一个日期存储为UNIX时间戳。然后,只要您想查看是否已过7天,就可以将当前时间戳(以秒为单位)与最早保存的旧时间戳进行比较。

Calendar cal = Calendar.getInstance(Locale.getDefault());
long firstTimestampInSec = cal.getTimeInMillis() / 1000;

然后,您可以通过查看是否超过604800秒来进行比较。

Calendar cal = Calendar.getInstance(Locale.getDefault());
long currentTimestampInSec = cal.getTimeInMillis() / 1000;
int oneWeekAsSec = 604800;

long firstTimestampInSec = getOldStoredTimestamp(); //First stored timestamp as seconds
if(currentTimestampInSec >= firstTimestampInSec + oneWeekAsSec){
    System.out.println("It's been a week");
}
else{
    System.out.println("It hasn't been a week");
}