【问题标题】:Java timer, update every secondJava定时器,每秒更新一次
【发布时间】:2014-12-29 05:36:54
【问题描述】:

我想从系统中获取当前日期和时间,我可以使用此代码:

    private void GetCurrentDateTimeActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date date = new Date();
    CurrentDateTime.setText(dateandtime.format(date));
}                                                  

这样做很好,因为它会抓住当前日期和时间,没有问题,但是它不是动态的,因为除非再次按下按钮,否则时间不会更新。所以我想知道如何通过每秒更新函数来刷新时间来使这个按钮更加动态。

【问题讨论】:

标签: java swing date time timer


【解决方案1】:

您可以使用executor 定期更新。像这样的:

ScheduledExecutorService e= Executors.newSingleThreadScheduledExecutor();
e.scheduleAtFixedRate(new Runnable() {
  @Override
  public void run() {
    // do stuff
    SwingUtilities.invokeLater(new Runnable() {
       // of course, you could improve this by moving dateformat variable elsewhere
       DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
       Date date = new Date();
       CurrentDateTime.setText(dateandtime.format(date));
    });
  }
}, 0, 1, TimeUnit.SECONDS);

【讨论】:

  • 感谢您的帮助!这完美地工作;但是,为了帮助其他人,应该调用 @Override 以便代码可以正常运行:)
【解决方案2】:

Swing 计时器(javax.swing.Timer 的一个实例)在指定的延迟后触发一个或多个动作事件。 参考: http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html
This answer might be useful for you

【讨论】:

    【解决方案3】:

    为此使用摇摆计时器:

    DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Timer t = new Timer(500, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Date date = new Date();
            CurrentDateTime.setText(dateandtime.format(date));
            repaint();
        }
    });
    t.start();
    

    【讨论】:

      【解决方案4】:

      首先定义一个TimerTask

      class MyTimerTask extends TimerTask  {
          JLabel currentDateTime;
      
           public MyTimerTask(JLabel aLabel) {
               this.currentDateTime = aLabel;
           }
      
           @Override
           public void run() {
               SwingUtilities.invokeLater(
                       new Runnable() {
      
                          public void run() {
                              // You can do anything you want with 'aLabel'
                               DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                               Date date = new Date();
                               currentDateTime.setText(dateandtime.format(date));
      
                          }
                      });
           }
      }
      

      然后,您需要在启动应用程序或 UI 时创建一个 java.util.Timer。例如你的 main() 方法。

      ...
      
      Timer timer = new Timer();
      timer.schedule(new MyTimerTask(label), 0, 1000);
      
      ...
      

      【讨论】:

      • 这是错误同步的;它从定时器的线程访问JLabel
      • @MKAftab 垃圾神评论是正确的;此示例代码非常不正确。在后台线程上操作 Swing 组件会导致奇怪的行为并且可能会失败。正确学习how to handle concurrency in Swing。要么修改此代码,要么撤回答案。
      • @Basil 你是对的。我编辑了我的答案。现在每件事都经过 EDT。但现在,我认为,使用 Swing-Timer 是一种更方便的解决方案。
      • 看起来不错。现在用Joda-Time 替换 java.util.Date 和 SimpleDateFormat 的使用,这将是完美的。 ;-)
      猜你喜欢
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-16
      • 2017-11-27
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多