【发布时间】:2018-09-07 17:04:54
【问题描述】:
好的,我读到了这个:Check date with todays date
@sudocode 给出了这个代码:
Calendar c = Calendar.getInstance();
// set the calendar to start of today
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
// and get that as a Date
Date today = c.getTime();
// or as a timestamp in milliseconds
long todayInMillis = c.getTimeInMillis();
// user-specified date which you are testing
// let's say the components come from a form or something
int year = 2011;
int month = 5;
int dayOfMonth = 20;
// reuse the calendar to set user specified date
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
// and get that as a Date
Date dateSpecified = c.getTime();
// test your condition
if (dateSpecified.before(today)) {
System.err.println("Date specified [" + dateSpecified + "] is before today [" + today + "]");
} else {
System.err.println("Date specified [" + dateSpecified + "] is NOT before today [" + today + "]");
}
但是假设保存的日期是 28/01/2018 晚上 11:00,我在 28/01/2018 晚上 11:15 运行它,所以这段代码会告诉我保存的日期是之前当前日期。
我想要的是,如果保存的日期超过一天,代码应该只运行一个函数......(不是 24 小时,但实际上是一天或更早)让我们说保存日期是 27/01/2018晚上 11:00 并且当前日期是 28/01/2018 那么它应该运行.. 我该如何实现呢?
【问题讨论】:
-
仅供参考,
java.util.Date、java.util.Calendar和java.text.SimpleDateFormat等麻烦的旧日期时间类现在已被 java.time 类所取代。大多数 java.time 功能在 ThreeTen-Backport 项目中被反向移植到 Java 6 和 Java 7。在ThreeTenABP 项目中进一步适用于早期的Android。见How to use ThreeTenABP…。