Java TimeUnit Usage toMsTime(final String value)

Here you can find the source of toMsTime(final String value)

Description

Converts the passed string to a ms timestamp.

License

Apache License

Parameter

Parameter Description
value The string value to parse

Return

a ms timestamp

Declaration

public static long toMsTime(final String value) 

Method Source Code

//package com.java2s;
/*//w  ww  .  java2s  . co m
 * Copyright 2015 the original author or authors.
 *
 * 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 {
    /**
     * Converts the passed string to a ms timestamp.
     * If the parsed long has less than 13 digits, it is assumed to be in seconds.
     * Otherwise assumed to be in milliseconds.
     * @param value The string value to parse
     * @return a ms timestamp
     */
    public static long toMsTime(final String value) {
        final long v = (long) Double.parseDouble(value.trim());
        return digits(v) < 13 ? TimeUnit.SECONDS.toMillis(v) : v;
    }

    /**
     * Determines the number of digits in the passed long
     * @param v The long to test
     * @return the number of digits
     */
    public static int digits(final long v) {
        if (v == 0)
            return 1;
        return (int) (Math.log10(v) + 1);
    }
}

Related

  1. toBias(long offset)
  2. todayInMillis()
  3. toDays(Date date)
  4. todayStart()
  5. toHeaderValue(long timeoutNanos)
  6. toString(final Stopwatch watch)
  7. toString(long milliseconds)
  8. toUnixTime(Date date)
  9. unixToJavaTime(long utime)