Java Millisecond to Hour getMillisFromHours(String hours)

Here you can find the source of getMillisFromHours(String hours)

Description

Edited this method by adding a nested try catch block that will parse input when the use enters the time in ##h##m format, it will also handle any format in ##H##M, ##h, ##H, ##m, ##M.

License

Open Source License

Parameter

Parameter Description
hours a parameter

Return

long milliseconds

Declaration

public static long getMillisFromHours(String hours) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*  ww  w  . j  a v  a 2 s . c  o  m*/
     * Edited this method by adding a nested try catch block that will parse input
     * when the use enters the time in ##h##m format, it will also handle any format
     * in ##H##M, ##h, ##H, ##m, ##M. In order to maintain original functionality, I 
     * editied the catch block to perform the actions for parsing user input in ##h##m format.
     * In the case of invalid format, 0 is returned. 
     * 
     * @author Aaron Musengo
     * @param hours
     * @return long milliseconds 
     */
    public static long getMillisFromHours(String hours) {
        if (hours == null || hours.length() == 0) {
            return 0;
        }

        try {
            double numHours = Double.parseDouble(hours);
            if (numHours < 0.0) {
                return 0;
            }
            double millisDouble = (numHours * 3600 * 1000);
            return (long) millisDouble;
        } catch (NumberFormatException e) {
            String hour = new String();
            String minute = new String();
            String temp = new String();
            for (int i = 0; i < hours.length(); i++) {
                if (hours.charAt(i) == 'h' || hours.charAt(i) == 'H') {
                    hour = temp;
                    temp = "";
                } else if (hours.charAt(i) == 'm' || hours.charAt(i) == 'M') {
                    minute = temp;
                    temp = "";
                } else if (hours.charAt(i) < '0' || hours.charAt(i) > '9') {
                    return 0;
                } else {
                    temp += hours.charAt(i);
                }
            }

            Double numHours = 0.0;
            Double numMinutes = 0.0;
            if (hour.length() != 0) {
                numHours = Double.parseDouble(hour);
            } else {
                hour = "0";
            }
            if (minute.length() != 0) {
                numMinutes = Double.parseDouble(minute);
                numMinutes = numMinutes / 60.0;
            }
            numHours += numMinutes;
            double millisDouble = (numHours * 3600 * 1000);

            return (long) millisDouble;
        }
    }
}

Related

  1. getHoursFromMillis(long ms)
  2. getHoursIntoMilliseconds(int nbHours)
  3. getHourSpareFromMilliSecond(int milliSecond)
  4. getMilliSecondByHourAndMins(int hour, int min)
  5. getMillisFromHours(String hours)
  6. milliseconds(long p_days, long p_hours, long p_minutes, long p_hoursPerBusinessDay)
  7. millisecondsToHours(long ms)
  8. millisecondToHour(Long millisecond)
  9. millisToHours(double millis)