Java Time Convert to timeToLong(String inputStr)

Here you can find the source of timeToLong(String inputStr)

Description

time To Long

License

Apache License

Parameter

Parameter Description
inputStr a parameter

Declaration

public static long timeToLong(String inputStr) 

Method Source Code

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

public class Main {
    public static final long ONE_WEEK_IN_MILISECONDS = 604800000L;
    public static final long ONE_DAY_IN_MILISECONDS = 86400000L;
    public static final long ONE_HOUR_IN_MILISECONDS = 3600000L;
    public static final long ONE_MINUTE_IN_MILISECONDS = 60000L;
    public static final long ONE_SECOND_IN_MILISECONDS = 1000L;

    /**//  w  w  w  .  j  a  va  2 s . c om
     *
     * @param inputStr
     * @return
     */
    public static long timeToLong(String inputStr) {
        String newStr = inputStr.replaceAll(" ", ""); // Remove all whitespaces
        long time = 0;

        int weekIndex2;
        int dayIndex2;
        int hourIndex2;
        int minuteIndex2;
        int secondIndex2;

        if (newStr.indexOf('w') != -1) {
            boolean foundChar = false;
            int tempIndex = weekIndex2 = newStr.indexOf('w');
            while (tempIndex > 0 && !foundChar) {
                tempIndex--;
                if (!Character.isDigit(newStr.charAt(tempIndex))) {
                    tempIndex++;
                    break;
                }
            }
            time += Integer.parseInt(newStr
                    .substring(tempIndex, weekIndex2))
                    * ONE_WEEK_IN_MILISECONDS;
        }

        if (newStr.indexOf('d') != -1) {
            boolean foundChar = false;
            int tempIndex = dayIndex2 = newStr.indexOf('d');
            while (tempIndex > 0 && !foundChar) {
                tempIndex--;
                if (!Character.isDigit(newStr.charAt(tempIndex))) {
                    tempIndex++;
                    break;
                }
            }
            time += Integer
                    .parseInt(newStr.substring(tempIndex, dayIndex2))
                    * ONE_DAY_IN_MILISECONDS;
        }

        if (newStr.indexOf('h') != -1) {
            boolean foundChar = false;
            int tempIndex = hourIndex2 = newStr.indexOf('h');
            while (tempIndex > 0 && !foundChar) {
                tempIndex--;
                if (!Character.isDigit(newStr.charAt(tempIndex))) {
                    tempIndex++;
                    break;
                }
            }

            time += Integer.parseInt(newStr
                    .substring(tempIndex, hourIndex2))
                    * ONE_HOUR_IN_MILISECONDS;
        }

        if (newStr.indexOf('m') != -1) {
            boolean foundChar = false;
            int tempIndex = minuteIndex2 = newStr.indexOf('m');
            while (tempIndex > 0 && !foundChar) {
                tempIndex--;
                if (!Character.isDigit(newStr.charAt(tempIndex))) {
                    tempIndex++;
                    break;
                }
            }
            time += Integer.parseInt(newStr.substring(tempIndex,
                    minuteIndex2)) * ONE_MINUTE_IN_MILISECONDS;
        }

        if (newStr.indexOf('s') != -1) {
            boolean foundChar = false;
            int tempIndex = secondIndex2 = newStr.indexOf('s');
            while (tempIndex > 0 && !foundChar) {
                tempIndex--;
                if (!Character.isDigit(newStr.charAt(tempIndex))) {
                    tempIndex++;
                    break;
                }
            }
            time += Integer.parseInt(newStr.substring(tempIndex,
                    secondIndex2)) * ONE_SECOND_IN_MILISECONDS;
        }
        return time;
    }
}

Related

  1. timeToFloat(String aTimeDuration)
  2. timeToFromNow(long time)
  3. timeToIndex(String twentyFourHourTime)
  4. timeToInt(final String time)
  5. timeToJgo(long milliSeconds)
  6. timeToMs(int day, int hour, int min, int sec)
  7. timeToRad(int time)
  8. timeToTick(float time, float speed)
  9. timeToTicks(Long time)