Java Millisecond Convert toMilliseconds(Object value)

Here you can find the source of toMilliseconds(Object value)

Description

Converts a string to a milliseconds long, interpreting 'ms', 's', 'm', 'h' and 'd' suffixes.

License

LGPL

Parameter

Parameter Description
value The value

Return

The milliseconds

Declaration

public static long toMilliseconds(Object value) 

Method Source Code

//package com.java2s;
/**//w ww  . j a v  a2  s  .co m
 * Copyright 2009-2016 Three Crickets LLC.
 * <p>
 * The contents of this file are subject to the terms of the LGPL version 3.0:
 * http://www.gnu.org/copyleft/lesser.html
 * <p>
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly from Three Crickets
 * at http://threecrickets.com/
 */

public class Main {
    /**
     * Converts a string to a milliseconds long, interpreting 'ms', 's', 'm',
     * 'h' and 'd' suffixes. Numbers are simply rounded to an integer.
     * <p>
     * Fractions can be used, and are rounded to the nearest millisecond, for
     * example: "1.5d".
     * 
     * @param value
     *        The value
     * @return The milliseconds
     */
    public static long toMilliseconds(Object value) {
        if (value == null)
            return 0L;
        if (value instanceof Number)
            return ((Number) value).longValue();
        else {
            String s = value.toString();
            if (s.endsWith("ms"))
                return (long) Float.parseFloat(s.substring(0,
                        s.length() - 2));
            else if (s.endsWith("s"))
                return (long) (Float.parseFloat(s.substring(0,
                        s.length() - 1)) * 1000f);
            else if (s.endsWith("m"))
                return (long) (Float.parseFloat(s.substring(0,
                        s.length() - 1)) * 60000f);
            else if (s.endsWith("h"))
                return (long) (Float.parseFloat(s.substring(0,
                        s.length() - 1)) * 3600000f);
            else if (s.endsWith("d"))
                return (long) (Float.parseFloat(s.substring(0,
                        s.length() - 1)) * 86400000f);
            else
                return (long) Float.parseFloat(s);
        }
    }
}

Related

  1. toMilliSeconds(float t)
  2. toMilliseconds(long nanoSeconds)
  3. toMilliseconds(long nanoseconds)
  4. toMilliseconds(long second)
  5. toMilliseconds(long seconds, long nanos)
  6. toMillisFromNanos(long nanos)
  7. toMillisInfo(long ms)
  8. toMillisToDayTime(long mill)
  9. toMillisToHHMMSSTime(long timeMillis)