Java TimeUnit Usage stringToMillis(String string)

Here you can find the source of stringToMillis(String string)

Description

string To Millis

License

Apache License

Parameter

Parameter Description
string The string to convert to milliseconds

Return

the milliseconds representation of the string. Example: 01:30:15.040999 > 5415040.999

Declaration

public static double stringToMillis(String string) 

Method Source Code

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

Related

  1. sleepNanos(long nanoDuration)
  2. sleepQuietly(int seconds)
  3. sleepSeconds(int secs)
  4. string2Millis(String duration)
  5. string2Millis(String duration)
  6. timestampMillis()
  7. timeToHMS(final long time)
  8. TimeToString(int ticks)
  9. toBias(long offset)