Created
January 19, 2017 11:51
-
-
Save qvoid/2942880765493cf74d502ec883e396fd to your computer and use it in GitHub Desktop.
Millisecond to string #Java #Time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static String millisToString(long millis, boolean text) { | |
boolean negative = millis < 0; | |
millis = java.lang.Math.abs(millis); | |
millis /= 1000; | |
int sec = (int) (millis % 60); | |
millis /= 60; | |
int min = (int) (millis % 60); | |
millis /= 60; | |
int hours = (int) millis; | |
String time; | |
DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US); | |
format.applyPattern("00"); | |
if (text) { | |
if (millis > 0) | |
time = (negative ? "-" : "") + hours + "h" + format.format(min) + "min"; | |
else if (min > 0) | |
time = (negative ? "-" : "") + min + "min"; | |
else | |
time = (negative ? "-" : "") + sec + "s"; | |
} | |
else { | |
if (millis > 0) | |
time = (negative ? "-" : "") + hours + ":" + format.format(min) + ":" + format.format(sec); | |
else | |
time = (negative ? "-" : "") + min + ":" + format.format(sec); | |
} | |
return time; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment