2019-10-26 23:15:49 +02:00
|
|
|
package fr.pandacube.util.measurement;
|
2016-02-16 20:07:51 +01:00
|
|
|
|
|
|
|
public class TimeUtil {
|
2016-07-14 14:22:23 +02:00
|
|
|
public static String durationToString(long msec_time, boolean dec_seconde) {
|
2018-10-31 14:25:42 +01:00
|
|
|
boolean neg = msec_time < 0;
|
|
|
|
msec_time = Math.abs(msec_time);
|
2016-02-16 20:07:51 +01:00
|
|
|
int j = 0, h = 0, m = 0, s = 0;
|
|
|
|
long msec = msec_time;
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-02-16 20:07:51 +01:00
|
|
|
j = (int) (msec / (1000 * 60 * 60 * 24));
|
2016-07-14 14:22:23 +02:00
|
|
|
msec -= (long) (1000 * 60 * 60 * 24) * j;
|
2016-02-16 20:07:51 +01:00
|
|
|
h = (int) (msec / (1000 * 60 * 60));
|
2016-07-14 14:22:23 +02:00
|
|
|
msec -= (long) (1000 * 60 * 60) * h;
|
2016-02-16 20:07:51 +01:00
|
|
|
m = (int) (msec / (1000 * 60));
|
2016-07-14 14:22:23 +02:00
|
|
|
msec -= (long) (1000 * 60) * m;
|
2016-02-16 20:07:51 +01:00
|
|
|
s = (int) (msec / 1000);
|
2016-07-14 14:22:23 +02:00
|
|
|
msec -= (long) 1000 * s;
|
|
|
|
|
2016-02-16 20:07:51 +01:00
|
|
|
String result = "";
|
2016-07-14 14:22:23 +02:00
|
|
|
if (j > 0) result = result.concat(j + "j ");
|
|
|
|
if (h > 0) result = result.concat(h + "h ");
|
|
|
|
if (m > 0) result = result.concat(m + "m ");
|
|
|
|
if (s > 0 && !dec_seconde) result = result.concat(s + "s");
|
|
|
|
else if (dec_seconde && (s > 0 || msec > 0)) {
|
|
|
|
msec += s * 1000;
|
|
|
|
result = result.concat((msec / 1000D) + "s");
|
2016-02-16 20:07:51 +01:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
|
|
|
if (result.equals("")) result = "0";
|
2018-10-31 14:25:42 +01:00
|
|
|
result = result.trim();
|
|
|
|
if (neg)
|
|
|
|
result = "-" + result;
|
|
|
|
|
|
|
|
return result;
|
2016-02-16 20:07:51 +01:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
|
|
|
public static String durationToString(long msec_time) {
|
2016-02-16 20:07:51 +01:00
|
|
|
return durationToString(msec_time, false);
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-02-16 20:07:51 +01:00
|
|
|
}
|