Here you can find the source of stringToMillis(String string)
Parameter | Description |
---|---|
string | The string to convert to milliseconds |
public static double stringToMillis(String string)
//package com.java2s; /**/*from w w w. ja v a 2 s. c o m*/ * Copyright 2016 Charles-Eugene Loubao * <p/> * 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 * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * 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. */ import java.util.concurrent.TimeUnit; public class Main { /** * @param string The string to convert to milliseconds * @return the milliseconds representation of the string. Example: 01:30:15.040999 > 5415040.999 */ public static double stringToMillis(String string) { String[] tokens = string.split(":"); int secondsIndex = tokens.length - 1; int minutesIndex = secondsIndex - 1; int hoursIndex = minutesIndex - 1; double seconds = (Double.parseDouble(tokens[secondsIndex].replace( ",", "."))) * 1000d; long minutes = minutesIndex > 0 ? TimeUnit.MINUTES.toMillis(Long .parseLong(tokens[minutesIndex])) : 0; long hours = hoursIndex >= 0 ? TimeUnit.HOURS.toMillis(Long .parseLong(tokens[hoursIndex])) : 0; return seconds + minutes + hours; } }