0

I want to check if a week has passed (using Calendar Class) and if it has then I need to print a message.

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

Any suggestions or answers would be brilliant! Thanks.

jvarela
  • 3,744
  • 1
  • 22
  • 43
  • you can achieve this by finding the day from the current date and then check it is sunday or not if sunday that means week had been passed. – Vishal Sharma Nov 12 '18 at 21:01
  • A week since when? Since the last time you executed some task? Then you need to store the time you did that. – Ole V.V. Nov 13 '18 at 12:28
  • Possible duplicate of [Running task periodicaly(once a day/once a week)](https://stackoverflow.com/questions/8615543/running-task-periodicalyonce-a-day-once-a-week) – Ole V.V. Nov 13 '18 at 12:29

2 Answers2

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

} else {

         System.out.println("It hasn't been a week");
}
Sari Masri
  • 206
  • 1
  • 10
  • Hi @Sari Masri, thanks for the answer! What does "%" do? –  Nov 12 '18 at 21:02
  • it will return Number of days left so if it the last day 7%7 will be zero – Sari Masri Nov 12 '18 at 21:06
  • 1
    So assuming `day` is in the interval 1–7 inclusive, it’s technically the same as `if (day == Calendar.SATURDAY)`, only harder to decipher? (And if `lastCallDate` was `MONDAY`, that is, 2, the result would be completely nonsensical, I’m afraid.) – Ole V.V. Nov 13 '18 at 15:07
  • @OleV.V., thanks for the explanation! How would you do it? –  Nov 13 '18 at 15:36
  • @magicmass I’d either use the linked original question or like `LocalDate.now(ZoneId.of("Europe/Rome")).getDayOfWeek().equals(DayOfWeek.SATURDAY)`. With the reservation that I’m not sure I have understood the requirement from the question. – Ole V.V. Nov 13 '18 at 15:47
0

At first you can store the first date as a UNIX timestamp. Then you can compare the current timestamp in seconds with the old, first saved timestamp whenever you want to check if 7 days past or not like below.

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

Then you can compare by looking if 604800 seconds past or not.

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");
}
Erdem Savasci
  • 697
  • 5
  • 12
  • Hi @Erdem Savasci, thanks for the answer. What’s “getOldStoredTimeStamp”? Where did you call it from? –  Nov 13 '18 at 14:47
  • Hi @magicmass. I just wrote it as an example. You can store the timestamp second value in many ways. For example in Android, you can use SharedPreferences as a key-value holder. Then you can write a method like "getOldStoredTimeStamp" to return the old timestamp value and compare it with the current value. – Erdem Savasci Nov 13 '18 at 15:26