Here you can find the source of convertHoursMinutesSecondsToSeconds(String offset)
Parameter | Description |
---|---|
NumberFormatException | if String can't be converted |
static public int convertHoursMinutesSecondsToSeconds(String offset) throws NumberFormatException
//package com.java2s; //License from project: Apache License import java.util.*; public class Main { /**//from ww w. ja va 2 s.co m * Given a string of the form hh:mm:ss or mm:ss, returns the total seconds * * @throws NumberFormatException if String can't be converted * @return the total number of seconds that the string represents */ static public int convertHoursMinutesSecondsToSeconds(String offset) throws NumberFormatException { boolean isOK = true; StringTokenizer tokenizer = new StringTokenizer(offset, ":"); String token = null; int first = -1; int second = -1; int third = -1; try { token = tokenizer.nextToken(); } catch (NoSuchElementException e) { isOK = false; } try { first = Integer.parseInt(token); } catch (NumberFormatException e) { isOK = false; } try { token = tokenizer.nextToken(); } catch (NoSuchElementException e) { isOK = false; } try { second = Integer.parseInt(token); } catch (NumberFormatException e) { isOK = false; } if (tokenizer.hasMoreTokens()) { try { token = tokenizer.nextToken(); } catch (NoSuchElementException e) { isOK = false; } try { third = Integer.parseInt(token); } catch (NumberFormatException e) { isOK = false; } } if (!isOK) { throw new NumberFormatException(); } if (third == -1) { if ((first < 0) || (first > 59)) { throw new NumberFormatException(); } if ((second < 0) || (second > 59)) { throw new NumberFormatException(); } return ((first * 60) + (second * 1)); } else { if ((third < 0) || (third > 59)) { throw new NumberFormatException(); } if ((second < 0) || (second > 59)) { throw new NumberFormatException(); } return ((first * 3600) + (second * 60) + (third * 1)); } } }