Android: 如何使用日历检查一周是否过去 class
Android: How to check whether a week has passed using Calendar class
我想检查一周是否已经过去(使用 Calendar
Class),如果已经过去那么我需要打印一条消息。
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");
}
任何建议或答案都会很棒!谢谢
if (day %lastCallDate ==0){
System.out.println("It's been a week");
} else {
System.out.println("It hasn't been a week");
}
首先您可以将第一个日期存储为 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");
}
我想检查一周是否已经过去(使用 Calendar
Class),如果已经过去那么我需要打印一条消息。
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");
}
任何建议或答案都会很棒!谢谢
if (day %lastCallDate ==0){
System.out.println("It's been a week");
} else {
System.out.println("It hasn't been a week");
}
首先您可以将第一个日期存储为 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");
}