Java Milliseconds Parse getTimeAsMillis(String value)

Here you can find the source of getTimeAsMillis(String value)

Description

A time can be passed in as a long in millis, or with a unit on it, like '60s'.

License

Open Source License

Exception

Parameter Description
NumberFormatException an exception

Return

The value of the property, or what is in default.

Declaration

public static long getTimeAsMillis(String value) throws NumberFormatException 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from w  w w  .j a  va 2 s .c  o m*/
     * A time can be passed in as a long in millis, or with a unit on it, like '60s'.
     * Possible units are
     *   ms, millis seconds
     *   s, seconds
     *   m, minutes
     *   h, hours
     *
     * @return The value of the property, or what is in default.
     * @throws NumberFormatException
     */
    public static long getTimeAsMillis(String value) throws NumberFormatException {
        if (value.indexOf("ms") != -1) {
            return Long.parseLong(value.substring(0, value.indexOf("ms")));
        } else if (value.indexOf("s") != -1) {
            return 1000 * Long.parseLong(value.substring(0, value.indexOf("s")));
        } else if (value.indexOf("m") != -1) {
            return 1000 * 60 * Long.parseLong(value.substring(0, value.indexOf("m")));
        } else if (value.indexOf("h") != -1) {
            return 1000 * 60 * 60 * Long.parseLong(value.substring(0, value.indexOf("h")));
        } else {
            return 1000 * Long.parseLong(value);
        }
    }
}

Related

  1. changeMillSecond2Date(long millSeconds)
  2. getTimeFromMilliseconds(long ms)
  3. getTimeInMilliseconds(String timeString)
  4. getTimeMillis(long FileTimestamp)
  5. getTimeMillis(String time)