Java TimeUnit Usage string2Millis(String duration)

Here you can find the source of string2Millis(String duration)

Description

string Millis

License

Apache License

Declaration

public static long string2Millis(String duration) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.concurrent.TimeUnit;

public class Main {
    public static long string2Millis(String duration) {
        long durationMillis = 0;
        try {/* w w w.  ja v a  2 s  . c o m*/
            durationMillis = Long.parseLong(duration);
        } catch (NumberFormatException nfe) {
            int indexOfM = duration.toUpperCase().indexOf('M');
            if (indexOfM > 0) {
                durationMillis = Long.parseLong(duration.substring(0, indexOfM));
                durationMillis = TimeUnit.MINUTES.toMillis(durationMillis);
            } else {
                int indexOfS = duration.toUpperCase().indexOf('S');
                if (indexOfS > 0) {
                    durationMillis = Long.parseLong(duration.substring(0, indexOfS));
                    durationMillis = TimeUnit.SECONDS.toMillis(durationMillis);
                } else {
                    throw new IllegalArgumentException("Cannot parse string: '" + duration
                            + "' Supported formats: '321321' (millis), '3m' (minutes) or '75s' (seconds)");
                }
            }
        }
        return durationMillis;
    }
}

Related

  1. sleepMilliseconds(long milliseconds)
  2. sleepNanos(long nanoDuration)
  3. sleepNanos(long nanoDuration)
  4. sleepQuietly(int seconds)
  5. sleepSeconds(int secs)
  6. string2Millis(String duration)
  7. stringToMillis(String string)
  8. timestampMillis()
  9. timeToHMS(final long time)