Java Second Convert convertToSeconds(String timeStr)

Here you can find the source of convertToSeconds(String timeStr)

Description

convert To Seconds

License

Apache License

Declaration

public static float convertToSeconds(String timeStr) 

Method Source Code

//package com.java2s;
/*// w  w  w.j  a  v a2s.  c om
Copyright 2007 Creare Inc.
    
Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 
    
http://www.apache.org/licenses/LICENSE-2.0 
    
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.
*/

public class Main {
    public static float convertToSeconds(String timeStr) {
        String[] partsStr = unpack(timeStr, ":");

        float[] partsFl = new float[partsStr.length];
        for (int i = 0; i < partsFl.length; i++) {
            try {
                partsFl[i] = Float.valueOf(partsStr[i]).floatValue();
            } catch (NumberFormatException e) {
                partsFl[i] = 0;
            }
        }

        float totalSeconds = partsFl[partsFl.length - 1];
        if (partsFl.length > 1) {
            totalSeconds += 60 * (partsFl[partsFl.length - 2]);
        }
        if (partsFl.length > 2) {
            totalSeconds += 3600 * (partsFl[partsFl.length - 3]);
        }

        return totalSeconds;
    }

    public static String[] unpack(String packed, String delimitor) {
        String str[] = null;

        // test for null input
        if ((packed == null) || (packed.equals(""))) {
            str = new String[1];
            str[0] = new String("");
            return str;
        }

        // remove any final delimitor(s)
        int delimLength = delimitor.length();
        while (packed.endsWith(delimitor)) {
            if (packed.length() > delimLength) {
                packed = packed.substring(0, packed.length() - delimLength);
            } else {
                // degenerate case-- packed string is nothing
                // but delimitors!
                str = new String[1];
                str[0] = new String("");
                return str;
            }
        }

        // find all instances of delimitor
        int startIndex = 0;
        int arrayIndex = 1; // will yield size of output array
        int index = 0;
        while (true) {
            index = packed.indexOf(delimitor, startIndex);
            if (index == -1) {
                break;
            }
            if (index == startIndex) {
                // deals with repeated delimitors,
                // or delimitor at start of packed string
                startIndex = index + delimLength;
                continue;
            }
            startIndex = index + delimLength;
            arrayIndex++;
        }

        // construct array and unpack packed string, removing delimitors.
        str = new String[arrayIndex];
        startIndex = 0;
        arrayIndex = 0;
        boolean reachedLast = false;
        while (!reachedLast) {
            index = packed.indexOf(delimitor, startIndex);
            if (index == startIndex) {
                // ignore repeat terms or delimitor at
                // head of string
                startIndex = index + delimLength;
                continue;
            }
            if (index == -1) {
                // have reached end of string
                index = packed.length();
                reachedLast = true;
            }
            str[arrayIndex] = new String(packed.substring(startIndex, index));
            arrayIndex++;
            startIndex = index + delimitor.length();
        }
        return str;
    }
}

Related

  1. convertTimecodeToSeconds(String timecode)
  2. convertTimemilisecondsToHours(long time)
  3. convertTimeSecondsToHMS(long longSecs)
  4. convertTimeToSeconds(String a_time)
  5. convertToSeconds(String time)
  6. convertToTenthsOfASecond(long epochSeconds, int nanos)
  7. secondConvertToString(final long spentSeconds)
  8. seconds2microseconds(double secs)
  9. seconds2String(long seconds)