格式天数毫秒

时间:2012-10-14 23:10:16

标签: java

简短的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天之后,我希望它在一个多月前说出来......但是根据它是怎样的,例如,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");

1 个答案:

答案 0 :(得分:5)

你的上一次发言

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

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