Java Second timeToSeconds(String textTime)

Here you can find the source of timeToSeconds(String textTime)

Description

Parses the message for time and returns it in seconds

License

Open Source License

Parameter

Parameter Description
message Input in the HOUR:MIN:SEC format

Return

Seconds

Declaration

public static int timeToSeconds(String textTime) 

Method Source Code

//package com.java2s;
/*// ww w .jav  a 2s .com
 * Util.java
 * 
 * PrisonMine
 * Copyright (C) 2013 bitWolfy <http://www.wolvencraft.com> and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

public class Main {
    /**
     * Parses the message for time and returns it in seconds
     * @param message Input in the HOUR:MIN:SEC format
     * @return Seconds
     */
    public static int timeToSeconds(String textTime) {
        if (textTime.charAt(0) == ':')
            textTime = "0" + textTime;
        if (textTime.charAt(textTime.length() - 1) == ':')
            textTime = textTime + "0";

        String[] parts = textTime.split(":");
        int time = 0;

        try {
            if (parts.length == 3) {
                time += Integer.parseInt(parts[0]) * 3600;
                time += Integer.parseInt(parts[1]) * 60;
                time += Integer.parseInt(parts[2]);
            } else if (parts.length == 2) {
                time += Integer.parseInt(parts[0]) * 60;
                time += Integer.parseInt(parts[1]);
            } else if (parts.length == 1) {
                time += Integer.parseInt(parts[0]);
            } else
                return -1;
        } catch (NumberFormatException nfe) {
            return -1;
        }
        return time;
    }
}

Related

  1. timeSpanInSeconds(long time1, long time2)
  2. timeStampSeconds()
  3. timeStringToSeconds(String input)
  4. timeStringToSeconds(String time)
  5. timeToSeconds(String in)
  6. timeToSeconds(String time)
  7. trimMiliSeconds(String time)
  8. validateSecond(int second)
  9. valueOfSecond(long time)