2 つのカレンダー オブジェクトの差を秒単位で取得しようとしています。
最初のカレンダー オブジェクトは現在の呼び出し時刻であり、2 番目のカレンダー オブジェクトはアイテムの作成タイムスタンプです。
ここで、「作成タイムスタンプ + 数秒」が現在の呼び出し時刻カレンダー オブジェクト datetime よりも後か前かを知りたいと思います。
Calendar referenceCalendar = Calendar.getInstance();
Calendar compareCalendar = obj.getCreationDate();
compareCalendar.add(Calendar.SECOND, obj.getDuaration());
long differenceInSeconds = (compareCalendar.getTimeInMillis() - referenceCalendar.getTimeInMillis()) / 1000;
if (differenceInSeconds == 0) {
iter.remove();
} else if (differenceInSeconds > 0) {
iter.remove();
}
しかし、結果は常に「差 > 0」です。私が間違っていることはありますか?
どんな助けでも感謝します。
編集
カレンダーオブジェクト「obj.getCreationDate()」が日時を継続的に更新することがわかりました。
方法は非常に簡単です。
public void setCreationDate(Calendar date)
throws InvalidParameterException {
if (date == null) {
throw new InvalidParameterException(
"Creation date can not be null.");
} else {
this.creationDate = date;
}
}
そして、メソッドは1回だけ呼び出されます:
setCreationDate(Calendar.getInstance);
Calendar.getInstance は、保存された var に日時の更新を強制しますか? これが起こるように私には思えます。
-> 私のメソッドは本当に 1 回だけ呼び出されると確信しています