-1

I am trying to convert an excel formula into Kotlin code. But I am facing issue in converting a double into duration. The Excel displays in duration format. How do I convert to Duration format? I cant use Duration as API level is less than 26.

The result in 
Excel Duration format - 0:47:22
Excel Number format - 0.03288861121

I am able to get it in the number format. But unable to convert it into time duration.

halfer
  • 19,824
  • 17
  • 99
  • 186
Droidme
  • 1,223
  • 6
  • 25
  • 45
  • It's unclear exactly what you want as the output. Do you want the string `"0:47:22"`? The number is the number of days, so you can multiply that by `24*60*60` to get the number of seconds, and then format that as a string anyway you want. – Michael Mar 16 '18 at 16:19
  • This does not seem to be an issue with excel... Why tag it as such? – Solar Mike Mar 17 '18 at 10:26
  • You *can* use `Duration` from java.time (the modern Java date and time API) at your API level. Add ThreeTenABP to your Android project, see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). Then make sure to import from `org.threeten.bp`, for example `org.threeten.bp.Duration`. – Ole V.V. Mar 17 '18 at 15:06

1 Answers1

3

I am unsure whether the following is the best solution. You may be able to get something better from Excel somehow. In any case it works.

    double durationDoble = 0.03288861121;
    Duration dur = Duration.ofNanos((long) (durationDoble * TimeUnit.DAYS.toNanos(1)));
    System.out.println(dur);

This prints

PT47M21.576008544S

If you are not used to the Duration class and/or ISO 8601 format, it may look a bit funny, but it means 47 minutes 21.576 seconds, so if rounded to whole seconds it agrees with what you expected.

I cant use Duration as API level is less than 26.

Yes, you can use Duration from java.time (the modern Java date and time API) in API levels less than 26. Add ThreeTenABP to your Android project and make sure to import org.threeten.bp.Duration.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161