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.
if (day %lastCallDate ==0){
System.out.println("It's been a week");
} else {
System.out.println("It hasn't been a week");
}
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");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.