Java TimeUnit Convert toMillis(String timeUnitString, String timeValue)

Here you can find the source of toMillis(String timeUnitString, String timeValue)

Description

Converts timeValue in timeUnitString to milliseconds

License

Apache License

Parameter

Parameter Description
timeUnitString the time unit string to convert, such as DAYS or SECONDS
timeValue the time value to convert to milliseconds

Return

corresponding value in milliseconds or LONG.MIN_VALUE if timeUnitString is invalid Returning LONG.MIN_VALUE gives consistent beahvior with the java library

Declaration

public static long toMillis(String timeUnitString, String timeValue) 

Method Source Code

//package com.java2s;
/**/*from ww w .  j  ava 2  s.  c om*/
 * Copyright (C) 2014-2015 LinkedIn Corp. (pinot-core@linkedin.com)
 *
 * 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.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class Main {
    private static final Map<String, TimeUnit> TIME_UNIT_MAP = new HashMap<>();

    /**
     * Converts timeValue in timeUnitString to milliseconds
     * @param timeUnitString the time unit string to convert, such as DAYS or SECONDS
     * @param timeValue the time value to convert to milliseconds
     * @return corresponding value in milliseconds or LONG.MIN_VALUE if timeUnitString is invalid
     *         Returning LONG.MIN_VALUE gives consistent beahvior with the java library
     */
    public static long toMillis(String timeUnitString, String timeValue) {
        TimeUnit timeUnit = timeUnitFromString(timeUnitString);
        return (timeUnit == null) ? Long.MIN_VALUE : timeUnit.toMillis(Long.parseLong(timeValue));
    }

    /**
     * Turns a time unit string into a TimeUnit, ignoring case.
     *
     * @param timeUnitString The time unit string to convert, such as DAYS or SECONDS.
     * @return The corresponding time unit or null if it doesn't exist
     */
    public static TimeUnit timeUnitFromString(String timeUnitString) {
        if (timeUnitString == null) {
            return null;
        } else {
            return TIME_UNIT_MAP.get(timeUnitString.toUpperCase());
        }
    }
}

Related

  1. toMillis(final long interval, final TimeUnit tu)
  2. toMillis(int duration, TimeUnit unit)
  3. toMillis(long duration, TimeUnit timeUnit)
  4. toMillis(long duration, TimeUnit unit)
  5. toMillis(long time, TimeUnit unit)
  6. toNanos(long timeout, TimeUnit unit)
  7. toSeconds(long duration, TimeUnit timeUnit)
  8. toSeconds(long timeout, TimeUnit unit)
  9. toSeconds(long value, TimeUnit unit)