How to display the date and time "at runtime"?

1

I would like to know how to display the current date and time of the computer, and the time must be updated at run time. I thought of passing an object to a label, but it did not. I will not save that date, I just want it to be displayed, I wanted to put it in a label so I can position it anywhere later.

package teste;

import static java.awt.Frame.MAXIMIZED_BOTH;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Locale;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TelaSistema extends JFrame {

    public JLabel label = new JLabel();
    public JPanel painel = new JPanel();

    public TelaSistema() {
        setExtendedState(MAXIMIZED_BOTH);
        setTitle("Teste");

        GregorianCalendar calendar = new GregorianCalendar();

        label = new JLabel("Data aqui");

        painel.add(label);
        add(painel);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String args[]) {

        TelaSistema tela = new TelaSistema();
    }
}
    
asked by anonymous 04.02.2017 / 13:50

1 answer

3
  

Note: Always start the screen within the Event-Dispatch-Thread ,   because swing is not Thread-Safe , and the whole GUI needs to start in.   of this unique Thread. Nesta   answer better explains the   reason for this and possible problems that may occur. This Other   answer shows some   ways to start the application within this Thread.

Using the java.time package classes, you can do as below:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TelaSistema extends JFrame {

    public JLabel label = new JLabel();
    public JPanel painel = new JPanel();

    public TelaSistema() {
        setExtendedState(MAXIMIZED_BOTH);
        setTitle("Teste");

        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
        LocalDateTime timePoint = LocalDateTime.now();

        label = new JLabel(timePoint.format(fmt));

        painel.add(label);
        add(painel);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {

        java.awt.EventQueue.invokeLater(() -> new TelaSistema()); 

    }
}

The DateTimeFormatter class is responsible for formatting the date, and the LocalDateTime class is one of the new API classes for working with time.

And to get the date updated in real time, you can create a Timer that forces the label to update every time, in this case, 1 second. To make it easier to work with the label, I used a class that extends from JLabel :

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.swing.JLabel;
import javax.swing.Timer;

public class ClockLabel extends JLabel {

    public ClockLabel() {
        Timer t = new Timer(1000, e -> setText(getDateTime()));
        t.setInitialDelay(0);
        t.start();
    }

    private String getDateTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"));
    }
}

Add this class to your project and then just instantiate it:

label = new ClockLabel();

See a working example:

Irecommendreading this question to learn how to work with these new classes, they are optimized to work with time, as well as reading from this other answer explaining some reasons to avoid working with the old classes to manipulate dates.

    
04.02.2017 / 14:00