How to show current time in java?

3
long tMinTotal = System.currentTimeMillis() / (60 * 1000);
int tMinCurrent = (int) (tMinTotal % (24 * 60));
hours = tMinCurrent / 60;
minutes = tMinCurrent % 60;

I know this is how it is done but I do not understand what the second line does. Thanks.

    
asked by Luis Pelaez 29.10.2018 в 17:09
source

5 answers

4

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);
    
answered by 29.10.2018 в 17:21
2

It's simpler if you get them through LocalDateTime

LocalDateTime ahora= LocalDateTime.now();
    int año = ahora.getYear();
    int mes = ahora.getMonthValue();
    int dia = ahora.getDayOfMonth();
    int hora = ahora.getHour();
    int minutos = ahora.getMinute();
    int segundos = ahora.getSecond();

In your case what you do is calculate the hour and minutes through the current milliseconds.

    
answered by 29.10.2018 в 17:18
1

There are several ways to show the current time. I leave you some ways.

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); /2016/11/16 12:08:43

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal)); //2016/11/16 12:08:43
    
answered by 29.10.2018 в 17:17
1

Here I commented on each line.

//Obtengo los minutos "totales" de la fecha (le saco los milisegundos)
long tMinTotal = System.currentTimeMillis() / (60 * 1000);
//Obtengo el resto al dividir los minutos por los días, el cual corresponde a "todos" los minutos del día
//(o sea, sin son las 1:15, esta variable va a valor 75)
int tMinCurrent = (int) (tMinTotal % (24 * 60));
//Obtengo las horas que hay en los minutos
hours = tMinCurrent / 60;
//Obtengo el resto al dividir los minutos por las horas, lo que nos da los minutos
minutes = tMinCurrent % 60;

I hope you understand.

Good luck!

    
answered by 29.10.2018 в 17:21
0

I recommend you use the class Calendar .

In this way, you can quickly get the date and time.

To get the time you just have to write:

Calendar calendario = Calendar.getInstance();
int hora, minutos, segundos;
hora = calendario.get(Calendar.HOUR_OF_DAY);
minutos = calendario.get(Calendar.MINUTE);
segundos = calendario.get(Calendar.SECOND);
System.out.println(hora + ":" + minutos + ":" + segundos);
    
answered by 29.10.2018 в 19:28