Time is not updated

3

I have a class in which I want to get the current time of the pc but it is not updated, it is always the same time, any solution?

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Hora extends Thread{

Calendar c = new GregorianCalendar();
int hora, minutos, segundos;   

@Override
public void run() {
    while(true){
        setHora();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Hora.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}      

 public void setHora() {
    String h;
    hora = c.get(Calendar.HOUR_OF_DAY);
    minutos = c.get(Calendar.MINUTE);
    segundos = c.get(Calendar.SECOND);
    h = hora + ":" + minutos + ":" + segundos;
    System.out.println(h);
}
}
    
asked by Steven Camargo 29.03.2018 в 01:36
source

2 answers

4

Greetings, Steven.

What happens is that you are instantiating your Calendar only once, and by default, at the moment of instantiation, if you do not assign any date, it will take the default date of the system at the moment of its instantiation:

Calendar c = new GregorianCalendar();

That is, when instantiating your class Hora , automatically your class Calendar will take the current date and time of your system, then when calling your method setHora , it will take that date and previous time .

If what you want is to obtain the current date and time of your system, then instantiate a new Calendar in your setHora method or assign your Calendar , updated date and time:

public void setHora() {
    c.setTime(new java.util.Date()); // coloqué toda la clase para que sepas a cual clase Date me refiero

    String h;
    hora = c.get(Calendar.HOUR_OF_DAY);
    minutos = c.get(Calendar.MINUTE);
    segundos = c.get(Calendar.SECOND);
    h = hora + ":" + minutos + ":" + segundos;
    System.out.println(h);
}

That way, you will assign to your Calendar the current date and time, which we obtain thanks to the class Date , another way, as I indicated, is instantiate again your Calendar :

public void setHora() {
    c = new GregorianCalendar();

    String h;
    hora = c.get(Calendar.HOUR_OF_DAY);
    minutos = c.get(Calendar.MINUTE);
    segundos = c.get(Calendar.SECOND);
    h = hora + ":" + minutos + ":" + segundos;
    System.out.println(h);
}

Here you have the link of the Java documentation for class Calendar .

    
answered by 29.03.2018 / 02:15
source
3

You can do it in the following way:

  • You declare a variable of type Date in your method, which I have called getHora , to maintain a standard of naming convention.
  • For simplicity, we will format with SimpleDateFormat . There is no reason to use three variables and then concatenate them ...

The method would look like this:

public void getHora() {
    Date fechaHoy = Calendar.getInstance().getTime();
    String horaActual=new SimpleDateFormat("HH:mm:ss").format(fechaHoy);
    System.out.println(horaActual);    
}
  • In the main class, you create a Timer from which getHora is called every second. I can not prove it right now, but I think I read that it's better to use Timer than to use Thread .

Something like this:

   Timer t = new Timer();
    t.schedule(new TimerTask() {
        @Override public void run() {
            getHora();
         // myTextField.setText(hora retornada eventualmente); 
        }
    }, 0L, 1000L);

If you would like to display the information in TextField for example, you can make getHora() return a String with the already formatted data and change the value of the element within Timer .

    
answered by 29.03.2018 в 02:16