Java Second timeToSeconds(String in)

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

Description

Convert a time String to seconds

License

Open Source License

Parameter

Parameter Description
in The time String to convert

Return

Time code in seconds

Declaration

public static int timeToSeconds(String in) 

Method Source Code

//package com.java2s;
/*//  w ww . j a  va  2  s . c o  m
Copyright 2013, 2014 Jason LaFrance
    
This file is part of WTBBackend.
    
WTBBackend 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.
    
WTBBackend 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 WTBBackend.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Convert a time String to seconds
     * 
     * @param in
     *            The time String to convert
     * @return Time code in seconds
     */
    public static int timeToSeconds(String in) {
        // time strings are stored as HH:MM:SS
        int secs;

        // dirty implementation, but a little faster
        /*
         * if (in.length() == 8 && in.charAt(2) == ':' && in.charAt(5) == ':') {
         * mins = (in.charAt(0) & 0xF) * 36000 + (in.charAt(1) & 0xF) * 3600 +
         * (in.charAt(3) & 0xF) * 600 + (in.charAt(4) & 0xF) * 60 +
         * (in.charAt(6) & 0xF) * 10 + (in.charAt(7) & 0xF);
         * 
         * } else { mins = 0; }
         */
        // clean implementation
        String[] n = in.split(":");
        secs = 0;

        if (n.length == 3) {
            try {
                secs = Integer.parseInt(n[0]) * 3600 + Integer.parseInt(n[1]) * 60 + Integer.parseInt(n[2]);
            } catch (NumberFormatException e) {
                ;

            }
        }
        return secs;
    }
}

Related

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