Java消息到期检查

时间:2015-11-21 22:52:37

标签: java

我有一个简单的问题,但对我来说有点困惑。基本上,我要求当库存到达特定时间时,需要检查库存是否较旧,如果库存较旧则需要库存。

我已经使用单元测试实现了以下代码。你可以验证一下它对你好吗?基本上,当前时间是阈值,意味着应用程序是提前一段时间,因此当前时间已经减少。

import java.util.Date;

  public class Expiry {

  public Expiry() {

  }

  public boolean hasExpired(Date currentTime, Date receivedAt) {
    boolean isExpired = false;
    System.out.println("Current Time is : " + currentTime);
    System.out.println("Received At is :" + receivedAt);
    if (currentTime.before(receivedAt)) {
        System.out.println("Keep it");
        isExpired = true;
    }
    else {
        System.out.println("Garbage it");
    }
    return isExpired;
    }
    }

    public class ExpiryTest {


    @Test
    public void testKeepIt() {
    Expiry expiry = new Expiry();
    Calendar receivedAt = Calendar.getInstance();
    receivedAt.set(Calendar.DATE, 21);
    receivedAt.set(Calendar.MONTH, 10);
    receivedAt.set(Calendar.YEAR, 2015);
    receivedAt.set(Calendar.HOUR_OF_DAY, 21);
    receivedAt.set(Calendar.MINUTE, 00);
    Calendar currentTime = Calendar.getInstance();
    currentTime.set(Calendar.DATE, 21);
    currentTime.set(Calendar.MONTH, 10);
    currentTime.set(Calendar.YEAR, 2015);
    currentTime.set(Calendar.HOUR_OF_DAY, 20);
    currentTime.set(Calendar.MINUTE, 59);
    boolean isExpired = expiry.hasExpired(currentTime.getTime(),
            receivedAt.getTime());
    assertEquals(true, isExpired);
  }

  @Test
  public void testGarbageItEqualsTo() {
    Expiry expiry = new Expiry();
    Calendar receivedAt = Calendar.getInstance();
    receivedAt.set(Calendar.DATE, 21);
    receivedAt.set(Calendar.MONTH, 10);
    receivedAt.set(Calendar.YEAR, 2015);
    receivedAt.set(Calendar.HOUR_OF_DAY, 21);
    receivedAt.set(Calendar.MINUTE, 00);
    Calendar currentTime = Calendar.getInstance();
    currentTime.set(Calendar.DATE, 21);
    currentTime.set(Calendar.MONTH, 10);
    currentTime.set(Calendar.YEAR, 2015);
    currentTime.set(Calendar.HOUR_OF_DAY, 21);
    currentTime.set(Calendar.MINUTE, 00);
    boolean isExpired = expiry.hasExpired(currentTime.getTime(),
            receivedAt.getTime());
    assertEquals(false, isExpired);
    }

    @Test
    public void testGarbageItAgain() {
    Expiry expiry = new Expiry();
    Calendar receivedAt = Calendar.getInstance();
    receivedAt.set(Calendar.DATE, 21);
    receivedAt.set(Calendar.MONTH, 10);
    receivedAt.set(Calendar.YEAR, 2015);
    receivedAt.set(Calendar.HOUR_OF_DAY, 21);
    receivedAt.set(Calendar.MINUTE, 00);
    receivedAt.set(Calendar.SECOND, 01);
    Calendar currentTime = Calendar.getInstance();
    currentTime.set(Calendar.DATE, 21);
    currentTime.set(Calendar.MONTH, 10);
    currentTime.set(Calendar.YEAR, 2015);
    currentTime.set(Calendar.HOUR_OF_DAY, 21);
    currentTime.set(Calendar.MINUTE, 01);
    currentTime.set(Calendar.SECOND, 00);
    boolean isExpired = expiry.hasExpired(currentTime.getTime(),
            receivedAt.getTime());
    assertEquals(false, isExpired);
    }
    }

1 个答案:

答案 0 :(得分:0)

您是否可以通过启动jUnitTest来检查自己?

如果您知道currentTime Date和receivedAt Date将在同一时区,那就足够了。