Java显示当前时间 [英] Java display current time

查看:66
本文介绍了Java显示当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码可以显示我运行应用程序的当前日期和时间

  DateFormat dateFormat = new SimpleDateFormat(  yyyy / MM / dd HH:mm:ss); 
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));

现在它显示了我:2012/12/11 00:36:53当我运行它时。 / p>

但我希望它计算运行期间的时间。



因此,现在我以运行它为例00:37:53它显示了这次,但我想在开始时00:37:53而我在00:40:55停止运行。我希望它向我显示00:37:53、00:37:54、00:37:55等。



现在我该怎么做?

解决方案

如何使用计时器,例如 javax.swing.Timer ? (不要在导入中犯错误,有更多的Timer类。)

  public static void main(String ... args )引发InterruptedException {
最后的DateFormat dateFormat = new SimpleDateFormat( yyyy / MM / dd HH:mm:ss);
int间隔= 1000; // 1000 ms

new Timer(interval,new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
现在的日历= Calendar.getInstance ();
System.out.println(dateFormat.format(now.getTime()));
}
})。start();

Thread.currentThread()。join();
}

这将仅每秒执行一次ActionListener的主体,并打印当前时间



最后一行的 Thread.join 调用不是普遍必需的,仅是本示例块所需要的直到手动停止该过程为止。否则,它将立即停止。



在实际的应用程序中,如果它是Swing应用程序,则计时器应自行处理线程,因此您不必担心。






编辑



集成以上示例进入您的应用程序非常简单,只需将其添加到 initGUI 方法中,而不是将当前时间打印到System.out设置中,即可更改给定标签的文本:

  public void initGUI(){
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(800,600));
setLayout(null);

现在的日历= Calendar.getInstance();
tijd = new JLabel(dateFormat.format(now.getTime()));
tijd.setBounds(100,100,125,125);
window.add(tijd);

新Timer(1000,新ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
现在的日历= Calendar.getInstance();
tijd.setText(dateFormat.format(now.getTime()));
}
})。start();

pack();
}


I have a code which shows me the current date and time when I run my application

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));

Now it shows me: 2012/12/11 00:36:53 when I run it.

But I want it to count the time during running it.

So by example now when I run it on 00:37:53 it shows this time, but I want 00:37:53 at starting and I stop running it on 00:40:55 . I want that it shows me 00:37:53, 00:37:54, 00:37:55 and so on.

Now how can I do this?

解决方案

How about using a timer, such as javax.swing.Timer? (Do not make mistake in the import, there are more Timer classes.)

public static void main(String... args) throws InterruptedException {
    final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    int interval = 1000; // 1000 ms

    new Timer(interval, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Calendar now = Calendar.getInstance();
            System.out.println(dateFormat.format(now.getTime()));
        }
    }).start();

    Thread.currentThread().join();
}

This will simply execute the body of the ActionListener every second, printing the current time.

The Thread.join call on the last line is not universally necessary, it's just needed for this example piece of code to run until the process is manually stopped. Otherwise, it would immediately stop.

In a real application, in case it's a Swing app, then the timer should handle threading by itself, so you won't have to worry about it.


Edit

Integrating the above sample into your application is fairly simple, just add it into the initGUI method and instead of printing the current time to System.out set change the text of the given label:

public void initGUI() {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        
    setPreferredSize(new Dimension(800, 600));
    setLayout(null);

    Calendar now = Calendar.getInstance();
    tijd = new JLabel(dateFormat.format(now.getTime()));
    tijd.setBounds(100, 100, 125, 125);
    window.add(tijd);

    new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Calendar now = Calendar.getInstance();
            tijd.setText(dateFormat.format(now.getTime()));
        }
    }).start();

    pack();
}

这篇关于Java显示当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆