Convert time in 13y2m3d format To Long value - Java java.util

Java examples for java.util:Time Format

Description

Convert time in 13y2m3d format To Long value

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String inputStr = "java2s.com";
        System.out.println(timeToLong(inputStr));
    }//from   w  w  w .j a va 2 s  .  c o m

    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;

    /**
     *
     * @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 Tutorials