Java TimeUnit Convert stringToTimeUnit(String str)

Here you can find the source of stringToTimeUnit(String str)

Description

string To Time Unit

License

Apache License

Declaration

public static TimeUnit stringToTimeUnit(String str) 

Method Source Code


//package com.java2s;
/*/*w  w w.  jav  a  2s. c  om*/
 *  * Copyright 2016 Skymind, Inc.
 *  *
 *  *    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 {
    public static TimeUnit stringToTimeUnit(String str) {
        switch (str.toLowerCase()) {
        case "ms":
        case "millisecond":
        case "milliseconds":
            return TimeUnit.MILLISECONDS;
        case "s":
        case "sec":
        case "second":
        case "seconds":
            return TimeUnit.SECONDS;
        case "min":
        case "minute":
        case "minutes":
            return TimeUnit.MINUTES;
        case "h":
        case "hour":
        case "hours":
            return TimeUnit.HOURS;
        case "day":
        case "days":
            return TimeUnit.DAYS;
        default:
            throw new RuntimeException("Unknown time unit: \"" + str + "\"");
        }
    }
}

Related

  1. getDateRelativeToNow(TimeUnit timeUnit, long amount)
  2. getRemainingTimeToday(final TimeUnit timeUnit)
  3. hoursToUnit(double hours, TimeUnit destinationUnit)
  4. millisecondsTo(long milliseconds, final TimeUnit timeUnit)
  5. millisToDuration(final int val, final TimeUnit unit)
  6. substract(Date date, long durationToSubstract, TimeUnit unit)
  7. timeToNanoSeconds(TimeUnit unit, long time)
  8. toIntervalFromNow(long timeback, TimeUnit unit)
  9. toMillis(Double sourceValue, TimeUnit sourceUnit)