Object type for data of type Time (Ex: 1 min and 56 seconds) in Android / Java?

5

I need to work with times, such as a timer (not my case), which at the end of its execution would have to save its value.

So I thought of some possibilities like:

  • java.utils.Date : only the data in the case is not a Data;
  • In long : saving the times in milliseconds, and convert it to the required formats whenever necessary; (if I went that way I found these two examples of conversions: this one right here in SOpt , and this )
  • Create my own object Tempo , which controls over milliseconds as in the previous option;

My question is as follows (Here is my question algorithm):

String pergunta = "Há alguma forma/tipo no Java que seja adequada a esse tipo de informação"
                + ", que não seja uma forma adaptativa como no caso de armazenar em um long?";
boolean resposta = fazerPerguntaComRespotaBoolean(pergunta);
String perguntaSecundaria = null;
if(resposta == true){
    perguntaSecundaria = "Qual seria? E como utilizar?";
} else {
    perguntaSecundaria = "Qual forma adaptativa é mais 'adaptativa' e esse tipo de dados?";
}

String respotaSecundaria = fazerPerguntaComRespotaDissertativa(perguntaSecundaria);
System.out.println(respotaSecundaria);
  

If you do not know what to do,

    
asked by anonymous 13.11.2014 / 13:57

2 answers

1

Recently I've been using date4j in an android project.

I also looked at Joda Time, but it seems to be very robust in terms of the size and complexity to use in an Android application. But this may be relative, because sometimes the application is so large that this difference may be insignificant.

I've also been searching, and it has performance issues at startup: link and link . These links may no longer represent reality, but it was a point that influenced my decision at the time.

Date4j is much lighter, even though it is not so complete (this depends on your need, mine has been met), as well as Joda Time . I enjoyed using the library, and it looks like it has several performance advantages.

I think it's not worth putting examples of use here, if need be I can put more time. In the official site has good documentation, with examples up to.

Like Joda Time, it also has a clone repository (the main author at first does not like maven) in Maven Central and jCenter, just add to your build.gradle pendence:

compile 'com.darwinsys:hirondelle-date4j:1.5.1'
    
14.11.2014 / 11:49
2

The most complete library to deal with this type of information is JodaTime . It is universally accepted by Java programmers as the solution when there are more complex time and date manipulation needs. Even when there is no need for such complexity it is interesting because it solves some problems and shortcomings of the official API.

Of course it is heavy for some standards. There are other libraries lighter, but less known and maybe if so reliable and with a guaranteed future. If you need something lightweight but that meets what you need and will use on a larger scale, do not rule out making your own library perhaps based on some already existing one. But you need to weigh well the cost (including the difficulties of doing right) X benefit.

Forget to use long pure.

    
13.11.2014 / 14:17