Java Minute Convert convertHoursMinutesSecondsToSeconds(String offset)

Here you can find the source of convertHoursMinutesSecondsToSeconds(String offset)

Description

Given a string of the form hh:mm:ss or mm:ss, returns the total seconds

License

Apache License

Exception

Parameter Description
NumberFormatException if String can't be converted

Return

the total number of seconds that the string represents

Declaration

static public int convertHoursMinutesSecondsToSeconds(String offset) throws NumberFormatException 

Method Source Code


//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));
        }
    }
}

Related

  1. convertDegreeMinuteSecond(double degree, double minute, double second)
  2. convertHoursToMinutes(Double hours)
  3. convertHoursToMinutes(final int hours)
  4. convertMillisToHoursMinutesSeconds(long offset)
  5. convertMinute(String targetHhMm)