Java Second Get getSecondsForShortString(String string)

Here you can find the source of getSecondsForShortString(String string)

Description

get Seconds For Short String

License

Apache License

Declaration

public static int getSecondsForShortString(String string) 

Method Source Code

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

public class Main {
    public static int getSecondsForShortString(String string) {
        int res = 0;
        string = string.trim();//from   w  w w.j a va 2s.  c o m

        String c;
        String curNum = "";
        for (int n = 0; n < string.length(); n++) {
            c = String.valueOf(string.charAt(n));
            if (c.matches("\\d")) {
                curNum += c;
            } else {
                int mul;
                switch (c.charAt(0)) {
                case 's':
                case 'S':
                    mul = 1;
                    break;
                case 'm':
                case 'M':
                    mul = 60;
                    break;
                case 'h':
                case 'H':
                    mul = 60 * 60;
                    break;
                case 'd':
                case 'D':
                    mul = 60 * 60 * 24;
                    break;
                default:
                    throw new NumberFormatException("Shorthand string does not allow using '" + c + "'");
                }
                res += Integer.parseInt(curNum) * mul;
                curNum = "";
            }
        }

        if (!curNum.isEmpty()) {
            res += Integer.parseInt(curNum);
        }

        return res;
    }
}

Related

  1. getSeconds(String s)
  2. getSeconds(String time)
  3. getSeconds(String time, int defaultValue)
  4. getSecondsAsClock(int time)
  5. getSecondsByDays(int days)
  6. getSecondsFromEpoch()
  7. getSecondsFromHMS(String hmsvalue)
  8. getSecondsFromString(String s)
  9. getSecondsFromString(String text)