My logic is as follow:
- Convert actual time to minutes.
- Extract seconds from minutes (30:04 -> 4 seconds).
- Subtract seconds value from minutes (step1-step2) to get time only in seconds.
- Convert time in minutes (step 3 output/60) to hours.
Now suppose i got time as 5404 which corresponds to 1:30:04. When I divide 5404, I get value as 90.0666666667. 0666666667 is actually 4 seconds to get value as 4 seconds. I convert the data to 60 unites by using percentage formula i.e
value * 60 / 100
I know there is a better solution here.
import java.math.BigDecimal;
import java.math.RoundingMode;
public class TimeCalc {
public static void main(String[] args) {
// Inital Time in seconds
BigDecimal actualTime = new BigDecimal(5404);
// 60 seconds
BigDecimal seconds = new BigDecimal(60);
// dividing original time to get time in mintues
BigDecimal timeinMinutes = actualTime.divide(seconds, 2, RoundingMode.CEILING);
// to get value of seconds from minutes
double secondsValue = timeinMinutes.doubleValue() - Math.floor(timeinMinutes.doubleValue());
// Seconds will be removed from Minutes to get only Minutes value
BigDecimal deductiableSecondValue = new BigDecimal(secondsValue);
timeinMinutes = timeinMinutes.subtract(deductiableSecondValue);
timeinMinutes = new BigDecimal(Math.ceil(timeinMinutes.doubleValue()));
// dividing minutes by 60 to get actual time in hours
BigDecimal timeinHours = timeinMinutes.divide(seconds, 2, RoundingMode.CEILING);
double hoursdecimalValue = timeinHours.doubleValue();
// suppose time is 1.30 , below code is to get the 30 minutes
double minutes = hoursdecimalValue - Math.floor(hoursdecimalValue);
// converting value in units of Time since it is always measured wrt 60
minutes = convertDateinTimeUnit(minutes);
// suppose time is 1.30 below code is get 1 value
String str = new Double(hoursdecimalValue).toString();
str = str.substring(0, str.indexOf('.'));
double v = Double.valueOf(str);
// converting value in units of Time since it is always measured wrt 60
secondsValue = convertDateinTimeUnit(secondsValue);
secondsValue = Math.ceil(secondsValue);
minutes = Math.ceil(minutes);
System.out.println(" secondsValue " + secondsValue);
System.out.println(" minutes decimal value " + minutes);
System.out.println(" hours " + v);
}
static double convertDateinTimeUnit(double value) {
double valueInTimeUnit = 60 * value / 100;
return valueInTimeUnit * 100;
}
}