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;
        duration = duration.toUpperCase().trim();
        int days = duration.indexOf('D');
        int hours = duration.indexOf('H');
        int minutes = duration.indexOf('M');
        int seconds = duration.indexOf('S');
        int lastIndex = 0;
        try {/*from w w  w . j  ava2  s . co m*/
            if (days > 0) {
                durationMillis += TimeUnit.DAYS
                        .toMillis(Long.parseLong(duration.substring(lastIndex, days).trim()));
                lastIndex = days + 1;
            }
            if (hours > 0) {
                durationMillis += TimeUnit.HOURS
                        .toMillis(Long.parseLong(duration.substring(lastIndex, hours).trim()));
                lastIndex = hours + 1;
            }
            if (minutes > 0) {
                durationMillis += TimeUnit.MINUTES
                        .toMillis(Long.parseLong(duration.substring(lastIndex, minutes).trim()));
                lastIndex = minutes + 1;
            }
            if (seconds > 0) {
                durationMillis += TimeUnit.SECONDS
                        .toMillis(Long.parseLong(duration.substring(lastIndex, seconds).trim()));
                lastIndex = seconds + 1;
            }
            if (lastIndex < duration.length()) {
                durationMillis += Long.parseLong(duration.substring(lastIndex));
            }
        } catch (NumberFormatException nfe) {
            throw new IllegalArgumentException("Cannot parse string: '" + duration + "'", nfe);
        }
        return durationMillis;
    }
}

Related

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