Converter handles HH:MM and MM:SS
This commit is contained in:
parent
96b0336b2c
commit
d0f617880c
|
@ -17,8 +17,8 @@ public class ConverterTest extends AndroidTestCase {
|
||||||
|
|
||||||
public void testGetDurationStringShort() throws Exception {
|
public void testGetDurationStringShort() throws Exception {
|
||||||
String expected = "13:05";
|
String expected = "13:05";
|
||||||
int input = 785000;
|
assertEquals(expected, Converter.getDurationStringShort(47110000, true));
|
||||||
assertEquals(expected, Converter.getDurationStringShort(input));
|
assertEquals(expected, Converter.getDurationStringShort(785000, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDurationStringLongToMs() throws Exception {
|
public void testDurationStringLongToMs() throws Exception {
|
||||||
|
@ -29,7 +29,7 @@ public class ConverterTest extends AndroidTestCase {
|
||||||
|
|
||||||
public void testDurationStringShortToMs() throws Exception {
|
public void testDurationStringShortToMs() throws Exception {
|
||||||
String input = "8:30";
|
String input = "8:30";
|
||||||
long expected = 510000;
|
assertEquals(30600000, Converter.durationStringShortToMs(input, true));
|
||||||
assertEquals(expected, Converter.durationStringShortToMs(input));
|
assertEquals(510000, Converter.durationStringShortToMs(input, false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,13 +74,14 @@ public final class Converter {
|
||||||
return String.format(Locale.getDefault(), "%02d:%02d:%02d", h, m, s);
|
return String.format(Locale.getDefault(), "%02d:%02d:%02d", h, m, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Converts milliseconds to a string containing hours and minutes */
|
/** Converts milliseconds to a string containing hours and minutes or minutes and seconds*/
|
||||||
public static String getDurationStringShort(int duration) {
|
public static String getDurationStringShort(int duration, boolean durationIsInHours) {
|
||||||
int minutes = duration / MINUTES_MIL;
|
int firstPartBase = durationIsInHours ? HOURS_MIL : MINUTES_MIL;
|
||||||
int rest = duration - minutes * MINUTES_MIL;
|
int firstPart = duration / firstPartBase;
|
||||||
int seconds = rest / SECONDS_MIL;
|
int leftoverFromFirstPart = duration - firstPart * firstPartBase;
|
||||||
|
int secondPart = leftoverFromFirstPart / (durationIsInHours ? MINUTES_MIL : SECONDS_MIL);
|
||||||
return String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
|
|
||||||
|
return String.format(Locale.getDefault(), "%02d:%02d", firstPart, secondPart);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Converts long duration string (HH:MM:SS) to milliseconds. */
|
/** Converts long duration string (HH:MM:SS) to milliseconds. */
|
||||||
|
@ -94,14 +95,20 @@ public final class Converter {
|
||||||
Integer.parseInt(parts[2]) * 1000;
|
Integer.parseInt(parts[2]) * 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Converts short duration string (HH:MM) to milliseconds. */
|
/**
|
||||||
public static int durationStringShortToMs(String input) {
|
* Converts short duration string (XX:YY) to milliseconds. If durationIsInHours is true then the
|
||||||
|
* format is HH:MM, otherwise it's MM:SS.
|
||||||
|
* */
|
||||||
|
public static int durationStringShortToMs(String input, boolean durationIsInHours) {
|
||||||
String[] parts = input.split(":");
|
String[] parts = input.split(":");
|
||||||
if (parts.length != 2) {
|
if (parts.length != 2) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return Integer.parseInt(parts[0]) * 60 * 1000 +
|
|
||||||
Integer.parseInt(parts[1]) * 1000;
|
int modifier = durationIsInHours ? 60 : 1;
|
||||||
|
|
||||||
|
return Integer.parseInt(parts[0]) * 60 * 1000 * modifier+
|
||||||
|
Integer.parseInt(parts[1]) * 1000 * modifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Converts milliseconds to a localized string containing hours and minutes */
|
/** Converts milliseconds to a localized string containing hours and minutes */
|
||||||
|
|
Loading…
Reference in New Issue