获取变量的最后更新时间 - java

时间:2015-06-21 01:35:44

标签: java for-loop last-modified

我想获得变量的最后更新时间。

static Date ti;
for (int i = 0; i <= attachedControllers.length; i++ )
{    ti = null;

    controllers tempc = attachedControllers[i];
     ti = tempc.getLastUpdated(); // return the last updated time of each controllers object.

    System.out.println("last updated of \t" + tempc.getControllerName() +"is \t"+ti);

    Date t2 = new Date();

    System.out.println("Time now \t" + t2);

    long s =    (t2.getTime() - ti.getTime())/(1000 * 60 );

System.out.println("difference is \t"+s);

}

问题是变量ti存储每个控制器对象的最后更新时间的所有先前值。我想只得到最后一个值。 这里的输出是:

last updated of     0.0.0.0/0.0.0.0:6632is  Sun Jun 21 03:38:59 AST 2015

Time now    Sun Jun 21 03:39:14 AST 2015

difference is   0

last updated of     0.0.0.0/0.0.0.0:6633is  Sun Jun 21 03:39:04 AST 2015

Time now    Sun Jun 21 03:39:19 AST 2015

difference is   0

last updated of     0.0.0.0/0.0.0.0:6632is  Sun Jun 21 03:38:59 AST 2015

Time now    Sun Jun 21 03:40:14 AST 2015

difference is   1

last updated of     0.0.0.0/0.0.0.0:6633is  Sun Jun 21 03:39:04 AST 2015

Time now    Sun Jun 21 03:40:19 AST 2015

difference is   1

last updated of     0.0.0.0/0.0.0.0:6632is  Sun Jun 21 03:40:59 AST 2015

Time now    Sun Jun 21 03:41:14 AST 2015

difference is   0

last updated of     0.0.0.0/0.0.0.0:6632is  Sun Jun 21 03:38:59 AST 2015 // here it retrieve the first value of last updated time which is my problem.

Time now    Sun Jun 21 03:41:14 AST 2015

difference is   2

last updated of     0.0.0.0/0.0.0.0:6633is  Sun Jun 21 03:39:04 AST 2015

Time now    Sun Jun 21 03:41:19 AST 2015

difference is   2

我将此函数称为:

ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
exec.scheduleAtFixedRate(new Runnable() 
       {
        public void run() 
          {
            checkControllerHealth ();
          }
       }, 15, 60, TimeUnit.SECONDS); 

1 个答案:

答案 0 :(得分:0)

然后将System.out.println放在你的循环之外。像这样:

static Date ti;
for (int i = 0; i <= attachedControllers.length; i++ )
{    ti = null;

    controllers tempc = attachedControllers[i];
     ti = tempc.getLastUpdated(); // return the last updated time of each controllers object.
}

 System.out.println("last updated of \t" + tempc.getControllerName() +"is \t"+ti);

 Date t2 = new Date();

 System.out.println("Time now \t" + t2);

 long s =    (t2.getTime() - ti.getTime())/(1000 * 60 );

 System.out.println("difference is \t"+s);

然后它将仅显示循环中的最后一次迭代。

我希望它会对你有所帮助!