How to display the time the program is running?

1

I wanted to know if there is a way to take the time the program is running, and display it on a label. From the moment it is run, it is "running a time", ie it shows exactly how long it is running.

public class ProgramaExemplo extends JFrame {

    public static void main(String args[]) {

        ProgramaExemplo programa = new ProgramaExemplo();
    }
    public JLabel label = new JLabel();

    public ProgramaExemplo() {
        setExtendedState(MAXIMIZED_BOTH);
        setTitle("---");

        JPanel x = new JPanel();
        x.setLayout(null);

        label = new JLabel("Tempo de execução .. ");
        x.add(label);
        label.setBounds(700, 500, 200, 200);
        add(x);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
}
    
asked by anonymous 04.02.2017 / 20:03

1 answer

4

To get to the solution below, it was only necessary to merge the solution from this answer with the other response , since the first one shows how to make a Timer and display it at runtime in a JLabel , and the second shows how to calculate the difference between times, using the new API, displaying from seconds to days:

import java.time.*;
import javax.swing.*;
import java.awt.event.*;
import java.time.temporal.ChronoUnit;

public class ProgramaExemplo extends JFrame {

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ProgramaExemplo();;
            }
        }); 

    }
    public JLabel label = new JLabel();

    public ProgramaExemplo() {
        setExtendedState(MAXIMIZED_BOTH);
        setTitle("---");

        JPanel x = new JPanel();
        x.setLayout(null);

        label = new ClockLabel();
        x.add(label);
        label.setBounds(700, 500, 300, 200);
        add(x);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    class ClockLabel extends JLabel implements ActionListener {

      private LocalDateTime start;

      public ClockLabel() {
        this.start = LocalDateTime.now();
        Timer t = new Timer(1000, this);
        t.start();
        setText(getDateTime(start));
      }

      private String getDateTime(LocalDateTime segundaDate){

        Duration testeDuration = Duration.between(start, segundaDate);
        long dias = testeDuration.toDays();
        Duration d2 = testeDuration.minus(dias, ChronoUnit.DAYS);
        long horas = d2.toHours();
        Duration d3 = d2.minus(horas, ChronoUnit.HOURS);
        long minutos = d3.toMinutes();
        Duration d4 = d3.minus(minutos, ChronoUnit.MINUTES);
        long segundos = d4.getSeconds();
        Duration d5 = d4.minus(segundos, ChronoUnit.SECONDS);
        long nanos = d5.toNanos();
        Duration d6 = d5.minus(nanos, ChronoUnit.NANOS);

        if (!d6.isZero()) throw new AssertionError(d6.toString());

        return "Total: " + dias + " dias, " + horas + " horas, " + minutos + " minutos, " + segundos + " segundos";
      }

      @Override
      public void actionPerformed(ActionEvent ae) {
        setText(getDateTime(LocalDateTime.now()));
      }
    }
}

Running:

Of course you will need to adjust the size of the label, since you are using AbsoluteLayout , because as time goes by, the string can increase its size.

    
04.02.2017 / 20:40