Suppose time is given in MM:SS(ex- 02:30) OR HH:MM:SS in String format.how can we convert this time to second.
3
- 2refer stackoverflow.com/questions/8826270/…Rince Thomas– Rince Thomas2015-03-26 07:21:04 +00:00Commented Mar 26, 2015 at 7:21
- 2or use JodaTime to parse to a duration: stackoverflow.com/questions/4619730/…allotria– allotria2015-03-26 07:24:46 +00:00Commented Mar 26, 2015 at 7:24
- 1also check stackoverflow.com/questions/15561580/…Rince Thomas– Rince Thomas2015-03-26 07:25:14 +00:00Commented Mar 26, 2015 at 7:25
Add a comment |
6 Answers
In your case, using your example you could use something like the following:
String time = "02:30"; //mm:ss String[] units = time.split(":"); //will break the string up into an array int minutes = Integer.parseInt(units[0]); //first element int seconds = Integer.parseInt(units[1]); //second element int duration = 60 * minutes + seconds; //add up our values If you want to include hours just modify the code above and multiply hours by 3600 which is the number of seconds in an hour.
Comments
public class TimeToSeconds { // given: mm:ss or hh:mm:ss or hhh:mm:ss, return number of seconds. // bad input throws NumberFormatException. // bad includes: "", null, :50, 5:-4 public static long parseTime(String str) throws NumberFormatException { if (str == null) throw new NumberFormatException("parseTimeString null str"); if (str.isEmpty()) throw new NumberFormatException("parseTimeString empty str"); int h = 0; int m, s; String units[] = str.split(":"); assert (units.length == 2 || units.length == 3); switch (units.length) { case 2: // mm:ss m = Integer.parseInt(units[0]); s = Integer.parseInt(units[1]); break; case 3: // hh:mm:ss h = Integer.parseInt(units[0]); m = Integer.parseInt(units[1]); s = Integer.parseInt(units[2]); break; default: throw new NumberFormatException("parseTimeString failed:" + str); } if (m<0 || m>60 || s<0 || s>60 || h<0) throw new NumberFormatException("parseTimeString range error:" + str); return h * 3600 + m * 60 + s; } // given time string (hours:minutes:seconds, or mm:ss, return number of seconds. public static long parseTimeStringToSeconds(String str) { try { return parseTime(str); } catch (NumberFormatException nfe) { return 0; } } } import org.junit.Test; import static org.junit.Assert.*; public class TimeToSecondsTest { @Test public void parseTimeStringToSeconds() { assertEquals(TimeToSeconds.parseTimeStringToSeconds("1:00"), 60); assertEquals(TimeToSeconds.parseTimeStringToSeconds("00:55"), 55); assertEquals(TimeToSeconds.parseTimeStringToSeconds("5:55"), 5 * 60 + 55); assertEquals(TimeToSeconds.parseTimeStringToSeconds(""), 0); assertEquals(TimeToSeconds.parseTimeStringToSeconds("6:01:05"), 6 * 3600 + 1*60 + 5); } @Test public void parseTime() { // make sure all these tests fail. String fails[] = {null, "", "abc", ":::", "A:B:C", "1:2:3:4", "1:99", "1:99:05", ":50", "-4:32", "-99:-2:4", "2.2:30"}; for (String t: fails) { try { long seconds = TimeToSeconds.parseTime(t); assertFalse("FAIL: Expected failure:"+t+" got "+seconds, true); } catch (NumberFormatException nfe) { assertNotNull(nfe); assertTrue(nfe instanceof NumberFormatException); // expected this nfe. } } } } Comments
try this
hours = totalSecs / 3600; minutes = (totalSecs % 3600) / 60; seconds = totalSecs % 60; timeString = String.format("%02d",seconds); 3 Comments
Rince Thomas
hi, he already have the hour minutes and seconds(HH:MM:SS ). he want to convert it to milliseconds. first read the question properly. then answer
DavidPostill
@Signare He asks "how can we convert this time to second". He does not say millisecond!
Bilal Şimşek
minutes = (totalSecs % 3600) / 60; I was struggling minutes part. this line solved my problem. thanks.
private static final String TIME_FORMAT = "hh:mm a";//give whatever format you want. //Function calling long timeInMillis = TimeUtils.getCurrentTimeInMillis("04:21 PM"); long seconds = timeInMillis/1000; //Util Function public static long getCurrentTimeInMillis(String time) { SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT, Locale.getDefault()); // sdf.setTimeZone(TimeZone.getTimeZone("GMT")); //getting exact milliseconds at GMT // sdf.setTimeZone(TimeZone.getDefault()); Date date = null; try { date = sdf.parse(time); } catch (ParseException e) { e.printStackTrace(); } return date.getTime(); } Comments
I have written an extension function in Kotlin for converting String to seconds
fun String?.converTimeToSeconds(): Int { if (this.isNullOrEmpty().not()) { val units = this?.split(":")?.toTypedArray() if (units?.isNotEmpty() == true && units.size >= 3) { val hours = units[0].toInt() val minutes = units[1].toInt() val seconds = units[2].toInt() return (3660 * hours) + (60 * minutes) + seconds } } return 0 }