Java - 基于毫秒的条件语句

时间:2016-07-25 14:07:38

标签: java date time conditional-statements

快速提问,我需要在特定时间段过后(以毫秒为单位)执行某种方法:

<style>
        body a {
            color: #2b2b2b;
        }
        body a:hover {
            color: #fff;
            text-decoration: none;
        }
        nav {
            min-height: 100px;
            background: #fff;
            color: #000;
            padding: 0;
            margin: 0;

        }
        nav a:hover {
            color: orange;
            text-decoration: none;
        }
        .navbar-toggle {
            background: cadetblue;
            color: #fff;
        }
        footer {
            background: #fff;
            color: #000;
            vertical-align: middle;
        }
        .container {
            background: transparent;
        }
        body a: hover {
            color: white;
            background: black;
        }
        .social-login-strip {
            width: 100%;
            height: inherit;
            background: #222;
            color: white;
            border-bottom: #000 thick solid;
        }
        .social-login-strip a {
            color: white;
            text-decoration: none;
        }
        .social-login-strip a:hover {
            color: white;
            text-decoration: none;
        }
        .white {
            padding: 20px 0px;
            color: #000;
            background: #fff;
            min-height: 100px;
        }
        .icon-bar {
            background: white;
        }
        .cari {
            min-height: 500px;
            height: inherit;
            top: 80px;
            background: transparent;
            color: white;
        }
        .cari a {
            color: white;
        }
        .cari a:hover {
            color: azure;
        }
        .sector {
            min-height: 600px;
            background: #222;
            color: white;
        }
        .background {
            max-width: 100%;
            max-height: 100%;
            position: fixed;
            z-index: -9999;
            background-size: contain;
        }
        #d-strip {
            min-height: 60px;
            background: url("images/img/6933687-abstract-artistic-art.jpg");
            background-size: cover;
        }
        .misc-links {
            height: 30px;
            background-color: orange;
        }
        .space {
            top: 0;
            left: 0;
            max-height: 100%;
            min-width: 100%;
            background: white;
        }
        .banner-prop {
            max-height: auto;
            max-width:100% ;

        }
        .centering {
            padding: 5% 0;
        }

        .child-centering {
            padding: 10% 0;
        }
        .strip {
            min-height: 30px;
            background: url("images/img/HD-Painting-Wallpaper-Desktop-Background-1.jpg");
        }
        .constrain {
            max-height: 500px;
            max-width: inherit;
        }
        .caption-blok {
            min-height: 30px;
            background: #2b2b2b;
            border-top-left-radius: 10px;
            border-top-right-radius: 10px;
            color: #fff;
            padding: 2px 5px;
        }
        .divide {
            background: transparent;
            min-height: 200px;
        }
        #linker {
            background-color: cadetblue;
            min-height: 300px;
            width: 100%;
            color: white;
            opacity: 0.9;
            -webkit-opacity: 0.9;

        }
        .bgbgor {
            background: orange;
        }
        #sign-up {
            border: #2b2b2b medium solid ;
            border-top-left-radius: 20px;
            border-top-right-radius: 20px;
            border-bottom-left-radius: 5px;
            border-bottom-right-radius: 5px;
        }
        .marg-min {
            margin: 20px;
        }
    </style>

然而,由于时间间隔的具体程度,我的程序从未触及过任何一个。

这样做的最佳方式是什么?

3 个答案:

答案 0 :(得分:1)

仅当currDate.getTime() == currDate.getTime() + incr01001时,此类表达式true才会评估为incr01001 == 0。出于同样的原因x == x + y仅在y == 0

答案 1 :(得分:1)

仅在特定时间段过后(以毫秒为单位)执行某种方法:

您可以使用Thread.sleep(long milliseconds)方法来执行此操作。

try{
  Thread.sleep(5000);//for 5 seconds
} catch(Exception ex){
  System.out.println("Error: "+ex.getMessage());
}

答案 2 :(得分:0)

首先尝试在while块之外获取时间:

Date date = new Date();
while (true)
{
  currDate = new Date();

  if (currDate.getTime() == date.getTime() + incrTrueBinaryErr)
    //do stuff
  //etc.
}

这样,while循环不会继续重新分配日期。

如你所说,它可能也有点过于具体,所以对于你的if块可能:

long timeDifference = date.getTime()+incrTrueBinaryErr-currDate.getTime();

if (Math.abs(timeDifference)<5l)
  //do stuff

如果新时间在你想要的5毫秒之内,“做点什么”。