4

I'm trying to compare a time with the time 24 hours ago.

This is what I have:

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;
    }
}

An example of input (these are converted from Strings to Dates):

null (this would return false)
Jul 11 at 19:36:47 (this would return false)
Jul 14 at 19:40:20 (this would return true)

This doesn't seem to be working. It always returns false. Any help would be appreciated!

Answer: In the end I kept getting false because "aDate" had no milliseconds, year, and other values that "pastDay" did.

To fix this I did the following:

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;
    }
Nikkisixx2
  • 148
  • 2
  • 15
  • With what input are you calling the method? – M A Jul 14 '14 at 20:21
  • @manouti The Date format is "MMM dd 'at' HH:mm:ss". If that was what you were asking. I never really thought about the format at all. That is probably where the problem is coming from. – Nikkisixx2 Jul 14 '14 at 20:23
  • 3
    What exactly do you mean "within the last 24 hours"? A strict interpretation would be "within the 86400 seconds preceding the time right now", but you could conceivably mean "within the calendar day 00:00 to 24:00 yesterday". – Jim Garrison Jul 14 '14 at 20:27
  • @JimGarrison In this case I mean "within the 86400 seconds preceding the time right now." – Nikkisixx2 Jul 14 '14 at 20:31
  • 1
    I think @manouti was asking what Date object you were passing to the method. If you're always passing in 1/1/1970 or null (for example), then you'd expect it to always return false, so we need to see what input you're passing it to try to help you. – Tim Jul 14 '14 at 20:38
  • @Tim I've added a couple examples of Dates that would be used. I handle nulls and I never get any incorrect dates. I can confirm that all inputs are valid. – Nikkisixx2 Jul 14 '14 at 20:43
  • It's amazing how many folks assure us that they've checked everything, and when you finally pry it out of them there's a glaring error. – Hot Licks Jul 14 '14 at 20:47
  • 1
    @HotLicks Not sure how a missing year is a "glaring error". I answered his question. I'm not getting null or and incorrect date. It's more or less just an incomplete date. Thanks for your useful input and help buddy. – Nikkisixx2 Jul 14 '14 at 20:50
  • When you're troubleshooting problems like this in the future, I'd highly recommend setting breakpoints in a debugger and inspecting the objects as you step through the code. By doing that, you could have seen the year of `aDate` and realized quickly what was going on. The debugger's ability to let you observe your objects and the path through your logic should be the first place you turn whenever something unexpected happens. – Tim Jul 14 '14 at 21:22
  • And those observed values should be a part of your question, if you write one. Unless you give your readers the input and output values they're having to fight with one hand tied behind their back (or whatever other analogy you'd like to apply). – Hot Licks Jul 14 '14 at 23:46
  • possible duplicate of [Compare if a date is less than 24 hours before](http://stackoverflow.com/questions/22423961/compare-if-a-date-is-less-than-24-hours-before) and [this](http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) and many others. – Basil Bourque Jul 15 '14 at 01:01

2 Answers2

11

How about using math?

static final long DAY = 24 * 60 * 60 * 1000;
public boolean inLastDay(Date aDate) {
    return aDate.getTime() > System.currentTimeMillis() - DAY;
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

So I tested this and it worked (Feel free to remove the debugging things) Make sure you are working with 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;
}

And this is the output:

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
Cyphrags
  • 528
  • 1
  • 7
  • 16