Java Milliseconds Parse getTimeInMilliseconds(String timeString)

Here you can find the source of getTimeInMilliseconds(String timeString)

Description

Convert a formatted time string to a count in milliseconds.

License

Open Source License

Parameter

Parameter Description
timeString a formatted time string representing hours, minutes, seconds & centiseconds (e.g. "1:05:17.32") or seconds & centiseconds (e.g. "1572.32").

Return

a time in milliseconds

Declaration

public static long getTimeInMilliseconds(String timeString) 

Method Source Code

//package com.java2s;
/**/*  w w  w  . j av  a 2s .co  m*/
 * Copyright 2005 British Broadcasting Corporation
 *
 * This file is part of the BBC R&D ID3v2 Chapter Tool application.
 *
 * The BBC R&D ID3v2 Chapter Tool application is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the License, 
 * or (at your option) any later version.
 *
 * The BBC R&D ID3v2 Chapter Tool application 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 Lesser General 
 * Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with 
 * the BBC R&D ID3v2 Chapter Tool application; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

public class Main {
    /**
     * Convert a formatted time string to a count in milliseconds. The method will accept time strings of the form:
     *         [s]s[.nn]
     *       [m]m:ss[.nn]
     *    [h]h:mm:ss[.nn]
     *    
     * or:
     *    
     *    N * s + [.nn]
     * 
     * @param timeString a formatted time string representing hours, minutes, seconds & centiseconds (e.g. "1:05:17.32") or seconds & centiseconds (e.g. "1572.32"). 
     * @return a time in milliseconds
     */
    public static long getTimeInMilliseconds(String timeString) {
        long hours = 0;
        long minutes = 0;
        long seconds = 0;
        long milliseconds = 0;
        int firstColonIndex = timeString.indexOf(":");
        int lastColonIndex = timeString.lastIndexOf(":");
        int periodIndex = timeString.lastIndexOf(".");

        if (periodIndex != -1) {
            // Determine centiseconds.
            if (timeString.length() - periodIndex == 2) {
                milliseconds = Integer.parseInt(timeString.substring(periodIndex + 1, periodIndex + 2)) * 100;
            } else if (timeString.length() - periodIndex == 3) {
                milliseconds = Integer.parseInt(timeString.substring(periodIndex + 1, periodIndex + 3)) * 10;
            } else if (timeString.length() - periodIndex > 3) {
                milliseconds = Integer.parseInt(timeString.substring(periodIndex + 1, periodIndex + 4));
            }
        } else {
            // There are no centiseconds.
            periodIndex = timeString.length();
        }

        if (lastColonIndex == -1) {
            // There are no hours or minutes. Seconds may exceed 59.
            seconds = Integer.parseInt(timeString.substring(0, periodIndex));
        } else {
            // Determine seconds.
            seconds = Integer.parseInt(timeString.substring(lastColonIndex + 1, periodIndex));
            // Determine minutes.
            if (firstColonIndex == lastColonIndex) {
                minutes = Integer.parseInt(timeString.substring(0, lastColonIndex));
            } else {
                minutes = Integer.parseInt(timeString.substring(firstColonIndex + 1, lastColonIndex));
            }
            // Determine hours.
            if (firstColonIndex != lastColonIndex && firstColonIndex != 0) {
                hours = Integer.parseInt(timeString.substring(0, firstColonIndex));
            }
        }

        long time = hours * 3600000L + minutes * 60000L + seconds * 1000L + milliseconds;
        return time;
    }
}

Related

  1. changeMillSecond2Date(long millSeconds)
  2. getTimeAsMillis(String value)
  3. getTimeFromMilliseconds(long ms)
  4. getTimeMillis(long FileTimestamp)
  5. getTimeMillis(String time)
  6. parseDate(long millisec)
  7. parseGMTInMillis(String time)