Method that returns Java date / time

1

After searching several posts related to the subject, I did not find an example where I create an "external" class (other than main for example) and main call the method that gives me the updated time automatically.

I have used the following way to retrieve date / time

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        scheduler.scheduleAtFixedRate(() -> {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy  HH:mm:ss");
            String sDataHora = sdf.format(new Date());
            jDataHora.setText(sDataHora);
        }, 1, 1, TimeUnit.SECONDS);
    
asked by anonymous 12.05.2017 / 21:44

1 answer

1

I do not know if I understood correctly but ...

public class ClasseUtilitaria {

    public static String horaAtualizada() {
        return new SimpleDateFormat("dd/MM/yyyy  HH:mm:ss").format(new Date());
    }

}


    public static void main(String[] args) throws InterruptedException {
        System.out.println(ClasseUtilitaria.horaAtualizada());
        Thread.sleep(1000);
        System.out.println(ClasseUtilitaria.horaAtualizada());
        Thread.sleep(1000);
        System.out.println(ClasseUtilitaria.horaAtualizada());
    }
    
12.05.2017 / 21:49