Java Second Convert convertToSeconds(String time)

Here you can find the source of convertToSeconds(String time)

Description

convert To Seconds

License

Apache License

Declaration

public static long convertToSeconds(String time) 

Method Source Code

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

public class Main {
    public static long convertToSeconds(String time) {
        time = time.toLowerCase();//from   w w  w . j a  va 2  s .com
        long seconds = 0;
        int index = 0;
        if (!(time.contains("s") || time.contains("m") || time.contains("h") || time.contains("d")
                || time.contains("w") || time.contains("y"))) {
            return -1;
        }
        while (!time.equals("")) {
            char ch = time.charAt(index);
            switch (ch) {
            case 's':
                seconds += Integer.parseInt(time.substring(0, index));
                if (time.length() > index + 1) {
                    time = time.substring(index + 1);
                    index = 0;
                } else {
                    return seconds;
                }
                break;
            case 'm':
                if (time.charAt(index + 1 >= time.length() ? index : index + 1) == 'o') {
                    seconds += Integer.parseInt(time.substring(0, index)) * 30 * 24 * 60 * 60;
                    if (time.length() > index + 2) {
                        time = time.substring(index + 2);
                        index = 0;
                    } else {
                        return seconds;
                    }
                } else {
                    seconds += Integer.parseInt(time.substring(0, index)) * 60;
                    if (time.length() > index + 1) {
                        time = time.substring(index + 1);
                        index = 0;
                    } else {
                        return seconds;
                    }
                }
                break;
            case 'h':
                seconds += Integer.parseInt(time.substring(0, index)) * 60 * 60;
                if (time.length() > index + 1) {
                    time = time.substring(index + 1);
                    index = 0;
                } else {
                    return seconds;
                }
                break;
            case 'd':
                seconds += Integer.parseInt(time.substring(0, index)) * 60 * 60 * 24;
                if (time.length() > index + 1) {
                    time = time.substring(index + 1);
                    index = 0;
                } else {
                    return seconds;
                }
            case 'w':
                seconds += Integer.parseInt(time.substring(0, index)) * 60 * 60 * 24 * 7;
                if (time.length() > index + 1) {
                    time = time.substring(index + 1);
                    index = 0;
                } else {
                    return seconds;
                }
            case 'y':
                seconds += Integer.parseInt(time.substring(0, index)) * 60 * 60 * 24 * 365;
                if (time.length() > index + 1) {
                    time = time.substring(index + 1);
                    index = 0;
                } else {
                    return seconds;
                }
            }
            index++;
        }
        return seconds;
    }
}

Related

  1. convertSecondsToTimeUnits(int seconds)
  2. convertTimecodeToSeconds(String timecode)
  3. convertTimemilisecondsToHours(long time)
  4. convertTimeSecondsToHMS(long longSecs)
  5. convertTimeToSeconds(String a_time)
  6. convertToSeconds(String timeStr)
  7. convertToTenthsOfASecond(long epochSeconds, int nanos)
  8. secondConvertToString(final long spentSeconds)
  9. seconds2microseconds(double secs)