It can be obtained in various ways,
using the class Date :
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
System.out.println("Hora actual: " + dateFormat.format(date));
using the class LocalDate :
LocalDateTime locaDate = LocalDateTime.now();
int hours = locaDate.getHour();
int minutes = locaDate.getMinute();
int seconds = locaDate.getSecond();
System.out.println("Hora actual : " + hours + ":"+ minutes +":"+seconds);
for both options, you get the format HH:mm:ss
, example of output:
Hora actual: 10:28:29
Get current time from System.currentTimeMillis ():
In your case I see that you try to get it from the value of System.currentTimeMillis()
therefore you have two options to get the current time UTC
:
1) Option using class TimeUnit
:
long millis = System.currentTimeMillis();
System.out.println("Hora actual: " + String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
));
2) This option is similar to the one you are trying to make:
long millis = System.currentTimeMillis();
int hours = (int) ((millis / (1000*60*60)) % 24);
int minutes = (int) ((millis / (1000*60)) % 60);
int seconds = (int) (millis / 1000) % 60 ;
System.out.println("Hora actual : " + hours + ":"+ minutes+":"+seconds);