Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.text.format.Time;

import java.util.Calendar;

import java.util.Locale;
import java.util.TimeZone;

public class Main {
    /***
     * Fetch the estimate current time of arrival at the destination
     * @param timeZone - The timezone at the destination
     * @param distance - how far to the target
     * @param speed - how fast we are moving
     * @return String - "HH:MM" current time at the target
     */
    public static String calculateEta(TimeZone timeZone, double distance, double speed) {

        // If no speed, then return an empty display string
        if (0 == speed) {
            return "--:--";
        }

        // fetch the raw ETE
        Time eteRaw = fetchRawEte(distance, speed, 0, true);

        // If the eteRaw is meaningless, then return an empty display string
        if (null == eteRaw) {
            return "--:--";
        }

        // Break the hours and minutes out
        int eteHr = eteRaw.hour;
        int eteMin = eteRaw.minute;

        // Hours greater than 99 are not displayable
        if (eteHr > 99) {
            return "XX:XX";
        }

        // Get the current local time hours and minutes
        int etaHr = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        int etaMin = Calendar.getInstance().get(Calendar.MINUTE);

        // Add in our ETE to the current time, accounting for rollovers
        etaMin += eteMin; // Add the estimated minutes enroute to "now"
        if (etaMin > 59) {
            etaMin -= 60;
            etaHr++;
        } // account for minute rollover
        etaHr += eteHr; // Now add the hours enroute
        while (etaHr > 23) {
            etaHr -= 24;
        } // account for midnight rollover

        // Format the hours and minutes
        String strHr = String.format(Locale.getDefault(), "%02d", etaHr);
        String strMn = String.format(Locale.getDefault(), "%02d", etaMin);

        // Build string of return
        return strHr + ":" + strMn;
    }

    /***
     * 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;
    }
}