Java Second Get getMSTimeInSeconds(final String timeStr)

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

Description

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

License

Open Source License

Parameter

Parameter Description
timeStr the input time string

Return

the number of seconds in the time

Declaration

public static int getMSTimeInSeconds(final String timeStr) 

Method Source Code

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

public class Main {
    /**//from w ww  .  j  a v a2 s  .c  o m
     * Parse a string of time (in minutes and seconds) and return
     * the time in seconds.  Acceptable input formats are SS, :SS
     * and MM:SS.
     * 
     * @param timeStr the input time string
     * @return the number of seconds in the time
     */
    public static int getMSTimeInSeconds(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 seconds
            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 seconds
            return getStringAsInteger(time.substring(1), 0, 0);
        }

        // There is a colon inside the string, so parse the
        // minutes (before) and seconds (after), and then add
        int mins = 60 * (getStringAsInteger(time.substring(0, colonIndex), 0, 0));
        int secs = getStringAsInteger(time.substring(colonIndex + 1), 0, 0);
        return (mins + secs);
    }

    /**
     * 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. getCurrentSecondZeroFillString()
  2. getCurrentTimeInSeconds()
  3. getCurrentTimeSeconds()
  4. getDayFromSeconds(long seconds)
  5. getFramesFromSeconds(Double seconds)
  6. getMultiplierForCmPerSecondsFromSpeedUnits(String speedUnits)
  7. getNbSecondsFromHour(int hour)
  8. getNiceStringForSeconds(String timeInSeconds)
  9. getNumberOfSecondsBetween(final double d1, final double d2)