Method that returns Java date/time

Asked

Viewed 509 times

1

After researching several posts referring to the subject, I did not find an example where I create an "external" class (other than the main one 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);

1 answer

1

I don’t know if I got it right 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());
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.