2

我有一个时间Timestamp格式,是一个产品的过期时间,我必须检查这个产品是否过期。我能怎么做?

我尝试过这种方式,但实际上该函数总是返回 TRUE(我不知道为什么)

public boolean isAuctionExpired() {

    // expiration_time is setted before, is the expiration date of a product!

    Timestamp actualTimeStampDate = null;

    try {
        Date actual = new Date();
        actualTimeStampDate = new Timestamp(actual.getTime());
    } catch (Exception e) {
        System.out.println("Exception :" + e);
    }

    boolean expired = (expiration_time.getTime() < actualTimeStampDate.getTime());

    // Everything seems ok, except the boolean
    System.out.println("product exp: "+expiration_time+", actual: "+actualTimeStampDate+" expired? "+expired);

    return expired;
}

我想我犯了一个愚蠢的错误,但我看不到它!

4

2 回答 2

7

你只需要

return expiration_time.before(new Date());

因为TimestampDate

如果它不起作用,那么值有问题expiration_time

于 2013-01-12T10:43:51.923 回答
0

只需使用以下代码:

public boolean isAuctionExpired() {
    return expiration_time.getTime() <= System.currentTimeMillis();
}
于 2013-01-12T10:49:59.753 回答