Java Minute Get getHMTimeInMinutes(final String timeStr)

Here you can find the source of getHMTimeInMinutes(final String timeStr)

Description

Parse a string of time (in hours and minutes) and return the time in minutes.

License

Open Source License

Parameter

Parameter Description
timeStr the input time string

Return

the number of minutes in the time

Declaration

public static int getHMTimeInMinutes(final String timeStr) 

Method Source Code

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

public class Main {
    /**/*from   w ww .j av  a 2  s.  c o m*/
     * Parse a string of time (in hours and minutes) and return
     * the time in minutes.  Acceptable input formats are MM, :MM
     * and HH:MM.
     * 
     * @param timeStr the input time string
     * @return the number of minutes in the time
     */
    public static int getHMTimeInMinutes(final String timeStr) {
        // Check the input
        if ((timeStr == null) || (timeStr.trim().length() < 1)) {
            // The input string is invalid
            return 0;
        }

        // Trim the string
        final String time = timeStr.trim();

        // Check for a colon
        final int colonIndex = time.indexOf(':');
        if (colonIndex < 0) {
            // There is no colon, so just parse the string as the
            // number of minutes
            return getStringAsInteger(time, 0, 0);
        } else if (colonIndex == 0) {
            // There is a colon at the start, so just parse the rest
            // of the string as the number of minutes
            return getStringAsInteger(time.substring(1).trim(), 0, 0);
        }

        // There is a colon inside the string, so parse the
        // hours (before) and minutes (after), and then add
        // together after multiplying the hours by 60 (to put
        // in minutes)
        int hrs = 60 * (getStringAsInteger(time.substring(0, colonIndex), 0, 0));
        int mins = getStringAsInteger(time.substring(colonIndex + 1), 0, 0);
        return (hrs + mins);
    }

    /**
     * Convert a string into an integer.
     * 
     * @param sInput the input string
     * @param defaultValue the default value
     * @param emptyValue the value to return for an empty string
     * @return the value as an integer
     */
    public static int getStringAsInteger(final String sInput, final int defaultValue, final int emptyValue) {
        // This is the variable that gets returned
        int value = defaultValue;

        // Check the input
        if (sInput == null) {
            return emptyValue;
        }

        // Trim the string
        final String inStr = sInput.trim();
        if (inStr.length() < 1) {
            // The string is empty
            return emptyValue;
        }

        // Convert the number
        try {
            value = Integer.parseInt(inStr);
        } catch (NumberFormatException nfe) {
            value = defaultValue;
        }

        // Return the value
        return value;
    }
}

Related

  1. fromMinutes(long seed)
  2. getCompleteDayAsHourMinuteString()
  3. getElapsedTimeHoursMinutesFromMilliseconds(long milliseconds)
  4. getFiveMinuteCronByHostId(long hostId)
  5. getFrequencyValueInMinutes(long value)
  6. getHourMinuteSecond(long misSecond)
  7. getHoursMinutesSeconds(float timeInSeconds)
  8. getLessThanOneMinuteAgoRendering()
  9. getMilliSecondFromMinute(int minute)