What is the correct way to get the device date?

Asked

Viewed 531 times

2

This is a relatively simple question, but one that has broken my mind a lot. I need to pick up the date and time at a certain point in my app’s run, but I don’t know what is the most recommended way to do this. In some places, I’ve seen them suggest using the API Time, others told me to use the Calendar, others told me to use the Date, some indicated me to format the output using SimpleDateFormat, others said that the SimpleDateFormat may present performance problems and I was told to use the DateFormat... anyway, I’m lost. I don’t know where to start. In this situation, which is the most indicated way?

  • 1

    Depends on what you intend to do :)

  • I just want to take the date in a simple and efficient way. As I saw several different opinions, different codes, I was confused. The idea is to take the date and store in a String, and in another String store the time.

2 answers

5


System.currentTimeMillis() is the most efficient if your main concern is performance. Date() should be close in terms of performance as it is only an encapsulator of a (long) value in thousandths of a second.

The class Calendar is comparatively slower and rather more complex resulting from the need to deal with all characteristics inherent in the representation of the date and time (leap years, adjustments for summer/winter time, different time zones, etc).

In conclusion:

  • if the main concern is the performance then suggested System.currentTimeMillis()`.
  • To perform calculations or format dates for the user would suggest the use of class Calendar due to its flexibility. It has a method getInstance which returns an implementation according to the regional definitions (region/geographical location) of the device. For example, if the device is configured for a location in Europe or America will return a calendar Gregorian.

    Its use is simple:

    Calendar c = Calendar.getInstance(); 
    Date data = c.getTime();
    

    The method getInstance returns a calendar initialized with current date and time according to local definitions of the device.

Finally, the class Team is declared obsolete since API Level 22 and already earlier, the API page itself warned against its use due to various problems that had been detected and recommended the use of the Calendar or Gregoriancalendar class.

This class has a number of issues and it is Recommended that Gregoriancalendar is used Instead.

  • In this case, if I just want to take the current date in a simple way, do I use Calendar? I wanted something easy to format, or that would already come formatted. Should I use Simpledateformat then? Performance is not a problem, it’s just what I saw them say around, unless the difference is absurd.

1

In practice uses all these classes, in this example generates two day and time strings, with the format for day 02-03-2018 and time string 22:21:30

    Calendar calendar = Calendar.getInstance();//cria o obj calendar e atribui a hora e data do sistema
    Date data = calendar.getTime();//transforma o obj calendar em obj Date

    SimpleDateFormat sddia = new SimpleDateFormat("dd-MM-yyyy");//cria um obj de formatação de data
    SimpleDateFormat sdhora = new SimpleDateFormat("HH:mm:ss");//cria um obj de formatação de hora
    String dia = sddia.format(data);//gera a string final formatada no estilo "dd-MM-yyyy"
    String hora = sdhora.format(data);//gera a string final formatada no estilo "HH:mm:ss"

Browser other questions tagged

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