Java Second timeStringToSeconds(String input)

Here you can find the source of timeStringToSeconds(String input)

Description

time String To Seconds

License

Open Source License

Declaration

public static int timeStringToSeconds(String input) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static int timeStringToSeconds(String input) {
        int result = 0;
        String number = "";
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (Character.isDigit(c)) {
                number += c;/*from  ww  w.  j av  a2s. c  o  m*/
            } else if (Character.isLetter(c) && !number.isEmpty()) {
                result += convert(Integer.parseInt(number), c);
                number = "";
            }
        }
        if (!number.equals("")) {
            result += Integer.parseInt(number);
        }
        return result;
    }

    private static long convert(int value, char unit) {
        switch (unit) {
        case 'd':
            return value * 60 * 60 * 24;
        case 'h':
            return value * 60 * 60;
        case 'm':
            return value * 60;
        case 's':
            return value;
        }
        return value;
    }
}

Related

  1. timeElapsedSecond(long prevTime, long curTime, long periodSecond)
  2. timeFromSeconds(int timeInSec)
  3. timeFromSeconds(long ms)
  4. timeSpanInSeconds(long time1, long time2)
  5. timeStampSeconds()
  6. timeStringToSeconds(String time)
  7. timeToSeconds(String in)
  8. timeToSeconds(String textTime)
  9. timeToSeconds(String time)