Java Second Convert timeStringToSeconds(String str)

Here you can find the source of timeStringToSeconds(String str)

Description

time String To Seconds

License

Open Source License

Declaration

public static int timeStringToSeconds(String str) throws ParseException 

Method Source Code


//package com.java2s;
/*//  w ww  . j a v a 2 s. c  om
 *  File: TimeHelper.java 
 *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
 *  A commercial license is available, see http://www.jaret.de.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 */

import java.text.ParseException;
import java.util.StringTokenizer;

public class Main {
    public static int timeStringToSeconds(String str) throws ParseException {
        try {
            StringTokenizer tokenizer = new StringTokenizer(str, ":");
            int h = Integer.parseInt(tokenizer.nextToken());
            int m = Integer.parseInt(tokenizer.nextToken());
            int s = 0;
            if (tokenizer.hasMoreTokens()) {
                s = Integer.parseInt(tokenizer.nextToken());
            }
            return h * 3600 + m * 60 + s;
        } catch (Exception e) {
            throw new ParseException(str, 0);
        }
    }
}

Related

  1. secondsToTimeString(int numSeconds)
  2. secondsToTimeString(long in_seconds)
  3. secondsToTimeString(long seconds)
  4. secondsToYWDHMS(long seconds)
  5. SecondToMin(double asec)
  6. toCentiSeconds(float t)
  7. toDaySecondDifference(long difference)
  8. toHhMmSs(long seconds)
  9. toNanoseconds(long sec, long nanos)