【问题标题】:How to check if X seconds has passed in Java?如何检查Java中是否已经过了X秒?
【发布时间】:2011-10-17 09:24:28
【问题描述】:

我知道.NET 我可以这样做:

DateTime test = DateTime.Now;
if (test >= (pastTime + TimeSpan.FromSeconds(15)) {
    doSomething();
}

Java 等价物是什么?

【问题讨论】:

    标签: c# java .net datetime time


    【解决方案1】:

    JDK8 以后的 java 时间库可以做这样的事情:

    import java.time.Duration
    import java.time.Instant
    
    class MyTimeClass {
        
        public static void main(String[] args) {
            Instant then = Instant.now();
            Duration threshold = Duration.ofSeconds(3);
            // allow 5 seconds to pass
            Thread.sleep(5000);        
            assert timeHasElapsedSince(then, threshold) == true;
        }
    
        public static boolean timeHasElapsedSince(Instant then, Duration threshold) {
            return Duration.between(then, Instant.now()).toSeconds() > threshold.toSeconds();
        }
    }
    

    【讨论】:

      【解决方案2】:

      对于这个简单的检查,我建议只使用时间戳(以毫秒为单位)而不是使用 java.util.Date 或其他一些类:

      long test = System.currentTimeMillis();
      if(test >= (pastTime + 15*1000)) { //multiply by 1000 to get milliseconds
        doSomething();
      }
      

      请注意,pastTime 变量也必须以毫秒为单位

      不幸的是,没有合适的“内置”java 类来处理时间跨度。为此,请查看Joda Time 库。

      更新:Java 8 引入了 java.time package,使用它来代替外部的 joda 时间。

      【讨论】:

      • 在我下面的答案中扩展了这个已接受答案对 Java 8 中 java.time 包的编辑。
      【解决方案3】:

      我能够通过在我的项目中使用 JodaTime 库来实现这一点。 我得到了这段代码。

      String datetime1 = "2012/08/24 05:22:34";
      String datetime2 = "2012/08/24 05:23:28";
      
      DateTimeFormatter format = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss");
      DateTime time1 = format.parseDateTime(datetime1);
      DateTime time2 = format.parseDateTime(datetime2);
      Minutes Interval = Minutes.minutesBetween(time1, time2);
      Minutes minInterval = Minutes.minutes(5);
      
      if(Interval.isGreaterThan(minInterval)){
        return true;
      }
      else{
        return false;
      }
      

      这将检查Time datetime1datetime2 isGreaterThan 5 Minutes 之间的间隔。将属性更改为Seconds。你会更容易知道。

      【讨论】:

        【解决方案4】:

        在我看来,你可以把它放在一个while循环中。我会以这种方式实现它。

        long initTime = System.currentTimeMillis();
        boolean timeElapsed = false;
        while(timeElapsed){
          if(System.currentTimeMillis - initTime > 15000 ){
            timeElapsed = true
          }else{
            doSomethingElse();
            Thread.sleep(500)
          }
        }
        doSomething()
        

        【讨论】:

        • Alex 的代码确实要等到 15 秒结束,它只检查 if 它们是否结束。当我第一次阅读它时也误解了这一点。
        • @f1sh 是的,这是真的......但通常当你检查一段时间是否结束时,你会重做它,直到这个时间真的结束......我没有看到用于检查的用例它只有一次。但事实上,这是我的看法;)
        • 它不是只检查一次,而是检查了多次,但在此期间会根据此检查执行其他操作。
        • @Alex 是的,你是对的......这就是我对 doSomethingElse() 的意思;)
        猜你喜欢
        • 2018-09-13
        • 2022-01-17
        • 2020-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-07
        相关资源
        最近更新 更多