0

简短的Java问题:

我有这个代码:

// 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");

你可以看到我在做什么。如果某件事发生在 24 至 48 小时前,则为“1 天前”,但如果发生在 48 小时至 1 个月(或 30 天)之间,则为“X 天* s * 前”。我不知道如何把这个月放在这种情况下。if如果我删除第二条语句的右侧......它会起作用。它会说 3 天前,或 15 天前,即 376 天前。显然,在 30 天后,我希望它说 1 个月前......但根据情况,例如 15 天前的内容不会在该子句中注册,它会跳过。我在这里错过了一些非常小的东西吗?

补充代码:

这是上下文中的全部内容;一切正常,直到“天”:

    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");
4

1 回答 1

5

你的最后一句话

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

永远不可能是真的,因为 (1000 * 60 * 60 * 24 * 30) 等于 -1702967296(又名溢出)。如果您真的坚持要这样做,请使用 BigInteger。但是,我强烈建议您重写代码。

于 2012-10-14T23:31:19.760 回答