Java tutorial
//package com.java2s; import android.text.format.Time; import java.util.Locale; public class Main { /*** * Fetch the estimate travel time to the indicated target * @param distance - how far to the target * @param speed - how fast we are moving * @param calculated - already calculated value * @param calc - or should I calculate? * @return String - "HH:MM" or "MM.SS" time to the target */ public static String calculateEte(double distance, double speed, long calculated, boolean calc) { // If no speed, then return the empty display value if (0 == speed && calc) { return "--:--"; } // Fetch the eteRaw value Time eteRaw = fetchRawEte(distance, speed, calculated, calc); // If an invalid eteRaw, then return the empty display value if (null == eteRaw) { return "--:--"; } // Break the eteRaw out into hours and minutes int eteHr = eteRaw.hour; int eteMin = eteRaw.minute; int eteSecond = eteRaw.second; // Hours greater than 99 are not displayable if (eteHr > 99) { return "XX:XX"; } // If hours is non zero then return HH:MM if (eteHr > 0) { // Format the hours and minutes en router String hr = String.format(Locale.getDefault(), "%02d", eteHr); String min = String.format(Locale.getDefault(), "%02d", eteMin); // Build the string for return return hr + ":" + min; } // Hours is zero, so return MM.SS String min = String.format(Locale.getDefault(), "%02d", eteMin); String sec = String.format(Locale.getDefault(), "%02d", eteSecond); // Build the string for return return min + "." + sec; } /*** * Fetch the raw estimated time enroute given the input parameters * @param distance - how far to the target * @param speed - how fast we are moving * @param calculated - already calculated value * @param calc - or should I calculate? * @return int value of HR * 100 + MIN for the ete, -1 if not applicable */ private static Time fetchRawEte(double distance, double speed, long calculated, boolean calc) { double eteTotal = calculated; if (calc) { // Calculate the travel time in seconds eteTotal = (distance / speed) * 3600; } // Allocate an empty time object Time ete = new Time(); // Extract the hours ete.hour = (int) (eteTotal / 3600); // take whole int value as the hours eteTotal -= (ete.hour * 3600); // Remove the hours that we extracted // Convert what's left to fractional minutes ete.minute = (int) (eteTotal / 60); // Get the int value as the minutes now eteTotal -= (ete.minute * 60); // remove the minutes we just extracted // What's left is the remaining seconds ete.second = Math.round((int) eteTotal); // round as appropriate // Account for the seconds being 60 if (ete.second >= 60) { ete.minute++; ete.second -= 60; } // account for the minutes being 60 if (ete.minute >= 60) { ete.hour++; ete.minute -= 60; } // Time object is good to go now return ete; } }