I’m trying to convert a running pace that I get in the format of a String (say : “5:00” for a 5 min/mile). (I am converting min/mile to min/km).
So here’s what I’m doing :
String milePace = "05:00";
DateFormat sdf = new SimpleDateFormat("mm:ss");
Date date = sdf.parse(milePace);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
long mileTime = cal.getTimeInMillis();
long kmPace = Math.round(mileTime * 0.623712);
cal.setTimeInMillis(kmPace);
This unfortunately makes sense to me, but does not work.
0.623712 would be the converting unit and does work (as long as I am concerned).
When I check the value of mileTime I get 18240000. As far I can see, this is the issue as this value should be 240000 (which would be 4mins * 60 secs * 1000)
Is there an issue with the code or is there something from the cal function that would give me that extra 18000000 miliseconds?
Thanks in advance!
getTimeInMillis()returns the number of milliseconds since 1970 UTC. It will be affected by the time zone of your parser, and what date the parser decides to use for your input.I would suggest that you use Joda Time and parse the value as a
Periodinstead.Sample code: