0

Brief Java question:

I have this code:

// edited above

} else if ((now >= (1000 * 60 * 60 * 24) && (now < (1000 * 60 * 60 * 48)))) {
            now = (now / (1000 * 60 * 60 * 24));
            time = String.valueOf(now + " day ago");


} else if ((now >= (1000 * 60 * 60 * 48) && (now < (1000 * 60 * 60 * 24 * 30)))) {
            now = (now / (1000 * 60 * 60 * 24));
            time = String.valueOf(now + " days ago");

You can see what I am doing. If something happened between 24-48 hours ago, it was "1 day ago", but if it was between 48 hours and 1 month (or 30 days) it was "X day*s* ago." I am not sure how to put the month in this situation. If I delete the right side of the second if statement... it works. It will say 3 days ago, or 15 days ago, of 376 days ago. Obviously, after 30 days, I want it to say over 1 month ago.... but based on how it is, something that is, for example, 15 days ago, will not register inside that clause and it will skip. Am I missing something really small here?

Supplementary code:

Here is the entire thing in context; everything works up until "days":

    lCDateTime = Calendar.getInstance();
    now = lCDateTime.getTimeInMillis();

    now = now - total;

    if (now <= ((1000 * 60 * 1))) {
        now = (now / 1000);
        time = String.valueOf(now + " seconds ago");

    } else if ((now > (1000 * 60 * 1)) && (now < 1000 * 60 * 2)) {
        now = (now / (1000 * 60));
        time = String.valueOf(now + " minute ago");

    } else if ((now >= (1000 * 60 * 2)) && (now < 1000 * 60 * 60)) {
        now = (now / (1000 * 60));
        time = String.valueOf(now + " minutes ago");

    } else if ((now >= (1000 * 60 * 60) && now < (1000 * 60 * 60 * 2))) {
        now = (now / (1000 * 60 * 60));
        time = String.valueOf(now + " hour ago");

    } else if ((now >= (1000 * 60 * 60 * 2) && (now < (1000 * 60 * 60 * 24)))) {
        now = (now / (1000 * 60 * 60));
        time = String.valueOf(now + " hours ago");

    } else if ((now >= (1000 * 60 * 60 * 24) && (now < (1000 * 60 * 60 * 48)))) {
        now = (now / (1000 * 60 * 60 * 24));
        time = String.valueOf(now + " day ago");

    } else if ((now >= (1000 * 60 * 60 * 48) && (now < (1000 * 60 * 60 * 24 * 30)))) {
        now = (now / (1000 * 60 * 60 * 24));
        time = String.valueOf(now + " days ago");
TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259
  • Is it possible that 'now' is an int and your values are simply getting too big? – Clayton Louden Oct 14 '12 at 23:16
  • @KickingLettuce And you're not missing any curly brackets? Have another look at the last else if statement – Clayton Louden Oct 14 '12 at 23:20
  • Why don't you just convert `now` into float days once and just compare it to 1.0, 2.0, etc, rather than repeating all the arithmetic that you're bound to confuse at some point? – Hot Licks Oct 14 '12 at 23:23
  • @Hot Licks IF you want to post that code for an answer I'll sure mark you correct. I am not a fan of all this redundant code for sure. – TheLettuceMaster Oct 14 '12 at 23:28

1 Answers1

5

Your last statement

else if ((now >= (1000 * 60 * 60 * 48) && (now < (1000 * 60 * 60 * 24 * 30))))

can never be true since (1000 * 60 * 60 * 24 * 30) equals -1702967296 (aka overflow). If you really insist on doing it like this, use BigInteger. However I strongly advise you to rewrite your code.

Clayton Louden
  • 1,056
  • 2
  • 11
  • 28