【问题标题】:get the last updated time of variable - java获取变量的最后更新时间 - java
【发布时间】:2015-06-21 01:35:43
【问题描述】:

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

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); 

【问题讨论】:

    标签: java for-loop last-modified


    【解决方案1】:

    然后将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);
    

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

    希望对你有帮助!

    【讨论】:

    • 它不打印任何东西:\
    • @user1888020 它给你一些错误?或者只是不打印任何东西?你能给我一个可以编译的代码版本吗?
    • 在for循环中添加break语句后,代码打印的输出与我上面写的一样,
    • @user1888020 尝试将代码的一行一行输入到循环中,看看它在哪一行打印了一些东西,请再次发表评论。
    • 问题是我的代码是三个关联在一起的eclipse项目。
    猜你喜欢
    • 1970-01-01
    • 2016-11-13
    • 2018-05-20
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多