Java 检查日期对象是否在过去24小时内发生
我试着把一个时间和24小时前的时间进行比较 这就是我所拥有的:Java 检查日期对象是否在过去24小时内发生,java,date,calendar,Java,Date,Calendar,我试着把一个时间和24小时前的时间进行比较 这就是我所拥有的: public boolean inLastDay(Date aDate) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_MONTH, -1); Date pastDay = cal.getTime(); if(aDate!= null) { if(aDate.after(pastDay)){
public boolean inLastDay(Date aDate) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);
Date pastDay = cal.getTime();
if(aDate!= null) {
if(aDate.after(pastDay)){
return true;
} else {
return false;
}
} else {
return false;
}
}
输入示例(从字符串转换为日期):
这似乎不起作用。它总是返回false。任何帮助都将不胜感激
答:最后我不断地得到false,因为“aDate”没有毫秒、年份和“pastDay”所具有的其他值
为了解决这个问题,我做了以下工作:
SimpleDateFormat sdfStats = new SimpleDateFormat("MMM dd 'at' HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -24);
Date yesterdayUF = cal.getTime();
String formatted = sdfStats.format(yesterdayUF);
Date yesterday = null;
try {
yesterday = sdfStats.parse(formatted);
} catch (Exception e) {
}
if(aDate!= null) {
if(aDate.after(yesterday)){
return true;
} else {
return false;
}
} else {
return false;
}
用数学怎么样
static final long DAY = 24 * 60 * 60 * 1000;
public boolean inLastDay(Date aDate) {
return aDate.getTime() > System.currentTimeMillis() - DAY;
}
所以我测试了它,它成功了(请随意删除调试内容) 确保您正在使用DateFormat
public static boolean inLastDay(java.util.Date aDate) {
java.util.Date today = DateFormat.getDateTimeInstance().getCalendar().getTime();
java.util.Date twentyfourhoursbefore = DateFormat.getDateTimeInstance().getCalendar().getTime();
twentyfourhoursbefore.setTime(twentyfourhoursbefore.getTime() - (24*60*60*1000));
System.out.println(DateFormat.getDateTimeInstance().format(today));
System.out.println(DateFormat.getDateTimeInstance().format(twentyfourhoursbefore));
System.out.println(DateFormat.getDateTimeInstance().format(aDate));
if(aDate.after(twentyfourhoursbefore) && aDate.before(today)){
return true;
}
return false;
}
这是输出:
Using a time within 24 hours:
14.07.1934 22:44:46
13.07.1934 22:44:46
13.07.1934 23:44:46
true
Using a time EXACTLY 24 hours before (it should also work with more than 24 hours):
14.07.1934 22:47:12
13.07.1934 22:47:12
13.07.1934 22:47:12
false
Using a time 24 hours after ATM:
14.07.1934 22:48:20
13.07.1934 22:48:20
15.07.1934 22:48:20
false
你用什么输入来调用这个方法?@manouti日期格式是“mmmdd”在“HH:mm:ss”。如果你是这么问的。我从来没有真正考虑过格式。这可能就是问题的根源。你说的“过去24小时内”到底是什么意思?严格的解释应该是“在现在时间之前的86400秒内”,但你可以想象的是“在昨天的日历日00:00到24:00之间”。我认为@manouti是在问你将什么日期对象传递给该方法。如果您总是输入1/1/1970或null(例如),那么您希望它总是返回false,因此我们需要查看您输入的内容以帮助您。@HotLicks不确定丢失的年份如何是“明显错误”。我回答了他的问题。我没有得到空或错误的日期。这或多或少只是一个不完整的日期。感谢您的宝贵意见和帮助。注意:这不会对夏令时的更改产生任何影响。i、 一天有时长达23小时或25小时。根据我目前的代码,这给了我相同的常量“false”。我想这与我的日期格式有关。@Nikkisixx2如果你不给出一年,它将假定它是1970年。
Using a time within 24 hours:
14.07.1934 22:44:46
13.07.1934 22:44:46
13.07.1934 23:44:46
true
Using a time EXACTLY 24 hours before (it should also work with more than 24 hours):
14.07.1934 22:47:12
13.07.1934 22:47:12
13.07.1934 22:47:12
false
Using a time 24 hours after ATM:
14.07.1934 22:48:20
13.07.1934 22:48:20
15.07.1934 22:48:20
false