Android Time to Milliseconds Convert timeStr2Secs(String s)

Here you can find the source of timeStr2Secs(String s)

Description

Converts a String in the format 00:00 to a long, being the number of seconds

License

GNU General Public License

Parameter

Parameter Description
s the String

Exception

Parameter Description
NumberFormatException if the given value cannot be interpreted as a time

Return

the total number of seconds (minutes + seconds * 60)

Declaration

public static long timeStr2Secs(String s) throws NumberFormatException 

Method Source Code

//package com.java2s;
/*//from   w  w  w  .  j a  v  a 2s.  c o  m
 * Copyright (C) 2013 Chuan-Zheng Lee
 *
 * This file is part of the Debatekeeper app, which is licensed under the
 * GNU General Public Licence version 3 (GPLv3).  You can redistribute
 * and/or modify it under the terms of the GPLv3, and you must not use
 * this file except in compliance with the GPLv3.
 *
 * This app 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 Licence for more details.
 *
 * You should have received a copy of the GNU General Public Licence
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Converts a String in the format 00:00 to a long, being the number of seconds
     * @param s the String
     * @return the total number of seconds (minutes + seconds * 60)
     * @throws NumberFormatException if the given value cannot be interpreted as a time
     */
    public static long timeStr2Secs(String s) throws NumberFormatException {
        long seconds = 0;
        String parts[] = s.split(":", 2);
        switch (parts.length) {
        case 2:
            long minutes = Long.parseLong(parts[0]);
            seconds += minutes * 60;
            seconds += Long.parseLong(parts[1]);
            break;
        case 1:
            seconds = Long.parseLong(parts[0]);
            break;
        default:
            throw new NumberFormatException();
        }
        return seconds;
    }
}

Related

  1. time2IntMillis(String timestr)
  2. time2LongMillis(String timestr)
  3. getTimeinMillis(int year, int month, int day)