-3

Folks!! I am super new to programming and I am creating an app to help out at my work. There is a calculation that is made that returns a result in time. It is a distance divided by speed. I have got as far as to get the result to show up in a TextView. But it is displayed as decimal. I need it to be displayed as 00:00 (minutes:seconds) and, if possible, 00:00:00 (hours:minutes:seconds) but only when time exceeds 60 minutes. If not, it would be great to omit the first "00" for "hour" just evoid visual pollution. So the exact numbers are: time = 217/9.5 which results 22.84210. I need this number to be displayed as 22:50.

See below the little I have got so far.... Please, note that I am at the very basics so it is very problable that you guys may see some heresies in my coding. Sorry for that.

 btn_vel_gal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                double val_gal = Integer.parseInt(velocidadegaleria.getText().toString());
                double val_tempo_d3_f1 = ( 217 / val_gal);
                tempo_d3_f1.setText(String.valueOf(val_tempo_d3_f1));
                DecimalFormat precision = new DecimalFormat("0.00");
                tempo_d3_f1.setText(precision.format(val_tempo_d3_f1));

val_gal is the variable where the 9.5 comes from.

Please, if any of you guys could be so kind as to help me. And please, be very didactic as to where exactly i should insert the piece of coding, showing exactly where i need to get from because I am haveing a hard time figuring out what components my coding lines need to get this to work.

Thank you all in advance

1 Answers1

0

java.time

I recommend that you use java.time, the modern Java date and time API, for your time work.

    double val_gal = Double.parseDouble("9.5");
    // Time in minutes with fraction of minute
    double val_tempo_d3_f1 = (217 / val_gal);

    // The Duration class can only work with whole numbers.
    // For greatest possible precision convert to nanoseconds
    long nanosInAMinute = Duration.ofMinutes(1).toNanos();
    long nanoseconds = Math.round(val_tempo_d3_f1 * nanosInAMinute);
    Duration dur = Duration.ofNanos(nanoseconds);
    
    // Convert to hours, minutes and seconds for display
    long hours = dur.toHours();
    dur = dur.minusHours(hours);
    long minutes = dur.toMinutes();
    dur = dur.minusMinutes(minutes);
    long seconds = dur.getSeconds();

    String formattedTime;
    if (hours == 0) {
        formattedTime = String.format("%02d:%02d", minutes, seconds);
    } else {
        formattedTime = String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }
    System.out.println(formattedTime);

Output is the desired:

22:50

Instead of the final System.out.println call you can do you tempo_d3_f1.setText(formattedTime);.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Hello, @OleV.V. That worked perfectly. Thank you so much for being so helpful. I was working on that problem for more than a week and frustration was already about to get me out of the game. And also thanks for the Integer to Double change. Really helpful. – Alessandro Silva Sep 27 '20 at 19:40