Java TimeUnit Calculate getTimeUnitByName(String timeUnit, TimeUnit defaultTimeUnit)

Here you can find the source of getTimeUnitByName(String timeUnit, TimeUnit defaultTimeUnit)

Description

get Time Unit By Name

License

Apache License

Declaration

public static TimeUnit getTimeUnitByName(String timeUnit, TimeUnit defaultTimeUnit) 

Method Source Code

//package com.java2s;
/*/*from   w ww  . j ava 2  s  . c  om*/
 *  Copyright 2008-2010 biaoping.yin
 *
 *  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 getTimeUnitByName(String timeUnit, TimeUnit defaultTimeUnit) {
        TimeUnit timeUnit_ = TimeUnit.SECONDS;
        if (timeUnit.equals("TimeUnit.SECONDS")) {

            timeUnit_ = TimeUnit.SECONDS;
        } else if (timeUnit.equals("TimeUnit.DAYS")) {
            try {
                timeUnit_ = TimeUnit.valueOf("DAYS");
            } catch (Exception e) {
                timeUnit_ = defaultTimeUnit;
            }
        }
        if (timeUnit.equals("TimeUnit.HOURS")) {
            try {
                timeUnit_ = TimeUnit.valueOf("HOURS");
            } catch (Exception e) {
                timeUnit_ = defaultTimeUnit;
            }
            //         timeUnit_ = TimeUnit.HOURS;
        } else if (timeUnit.equals("TimeUnit.MICROSECONDS")) {
            timeUnit_ = TimeUnit.MICROSECONDS;
        } else if (timeUnit.equals("TimeUnit.MILLISECONDS")) {
            timeUnit_ = TimeUnit.MILLISECONDS;
        } else if (timeUnit.equals("TimeUnit.MINUTES")) {
            try {
                timeUnit_ = TimeUnit.valueOf("MINUTES");
            } catch (Exception e) {
                timeUnit_ = defaultTimeUnit;
            }
        } else if (timeUnit.equals("TimeUnit.NANOSECONDS")) {
            timeUnit_ = TimeUnit.NANOSECONDS;
        }
        return timeUnit_;
    }
}

Related

  1. getTimeBucket(TimeUnit unit, long timestamp, int bucketSizeInSeconds)
  2. getTimeInMillis(TimeUnit unit)
  3. getTimeString(long value, TimeUnit unit)
  4. getTimeUnit(Object units)
  5. getTimeUnit(String timeUnit, TimeUnit defaultUnit)
  6. getValueString(long value, TimeUnit unit)
  7. getWindowFlooredBinaryTime(TimeUnit unit, long timestamp, int windowSizeInSeconds)
  8. humanReadableTime(long time, TimeUnit unit)
  9. isValidTimeUnit(String timeUnit)