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

Asked

Viewed 179 times

5

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

So I thought of some possibilities like:

  • java.utils.Date: only that the data in the case is not a Date;
  • In long: keeping the times in milliseconds, and converting it to the necessary formats whenever necessary; (if this is the way I found these two examples of conversions: this one right here at Sopt, and that)
  • Create my own object Tempo, that makes the control over milliseconds as in the previous option;

My doubt is the following (Follow 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);

Note: If anyone bothers, or if it became complicated to understand my question, I remove the algorithm format of my question, no problems, it is just to relax.

  • 3

    Is there a problem with https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html? Conjece o http://www.joda.org/joda-time/? You have read this https://docs.oracle.com/javase/tutorial/datetime/iso/datetime.html?

  • @bigown, did not know no, I already took a superficial look and it seems that will help, I will see how to work with times with these objects as quoted in the question, ex: 14h 23m 34s. Thanks for the tip.

2 answers

2

The most complete library to handle this type of information is the Jodatime. It is universally accepted by Java programmers as the solution when there are more complex time and date handling needs. Even when there is no need for so much complexity it is interesting because it solves some problems and deficiencies of the official API.

Of course she’s heavy by some standards. There are other libraries lighter, but less known and who knows if so reliable and with a guaranteed future. If you need something light but that meets what you need and will use on a larger scale, do not discard making your own library perhaps based on an existing one. But need to weigh well the cost (including the difficulties of doing right) X benefit.

Forget to use the long pure.

  • I never got to look at the revamp of the official API of date/time of Java 8 that seems to have been based on Jodatime, you consider that in Java 8 applications Jodatime is preferable to the API?

  • @Piovezan also did not see and so it is difficult to speak, I can only confirm what you said. For those who already use version 8, the new official API may be enough.

1


Recently I’ve been using the date4j on an android project.

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

I’ve also been researching and she has some performance problems at startup: https://stackoverflow.com/questions/5059663/android-java-joda-date-is-slow and https://github.com/christerdk/Malmofestivalen-for-Android/issues/6. These links may no longer represent reality, but it was a point that influenced me in the decision at the time.

The Date4j is much lighter, although not so complete (this depends on your need, mine was met), as to Joda Time. I enjoyed using the library, and it seems that it has several advantages over performance.

I think it is not worth putting examples of use here, if you need I can put in more time. No official website has good documentation, with examples up to.

Just like Joda Time, it also has a clone repository (the main author at first dislikes Maven) in Maven Central and jCenter, just add to your build.gradle dependence:

compile 'com.darwinsys:hirondelle-date4j:1.5.1'

Browser other questions tagged

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