Time parsing method

Discussion in 'Resources' started by p000ison, Oct 19, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    p000ison

    Heyho Community,
    I just wrote a method to parse a string, which holds a time, so I want to share it with you and maybe improve it.

    Code:
    private static final long DAY = 86400000L;
    private  static final long HOUR = 3600000L;
    private  static final long MINUTE = 60000L;
    private  static final long SECOND = 1000L;
     
    private static final char DAY_CHAR = 'd', HOUR_CHAR = 'h', MINUTE_CHAR = 'm', SECOND_CHAR = 's';
     
           /**
         * This parses a String which holds a time and returns the duration in ms.
         * <p/>
         * <p><strong>Example:</strong> 2d5h2m52s</p>
         *
         * @param time The string to parse
         * @return The duration in ms
         */
        public static long parseTime(String time)
        {
            char[] chars = time.toCharArray();
     
            long duration = 0;
     
            long current = 0;
            int run = 1;
            long multi = 1;
     
            //iterate backwards
            for (int i = chars.length - 1; i >= 0; i--) {
                char character = chars[i];
                int currentNumber = getDigit(character);
     
                if (currentNumber != -1) {
                    //is number
                    //set the value of the current iteration
                    current += currentNumber * run;
                    //add the current value to the whole
                    duration += current * multi;
                    //we set our run '* 10' because: 112 = 1 * 100 + 1 * 10 + 2 * 1
                    run *= 10;
                } else {
                    //is character
                    //lets find our multiplicator
                    switch (character) {
                        case DAY_CHAR:
                            multi = DAY;
                            break;
                        case HOUR_CHAR:
                            multi = HOUR;
                            break;
                        case MINUTE_CHAR:
                            multi = MINUTE;
                            break;
                        case SECOND_CHAR:
                            multi = SECOND;
                            break;
                        case ' ':
                        case '_':
                            continue;
                        default:
                            throw new IllegalArgumentException(String.format("Invalid character found! %s", character));
                    }
                    //everytime we get a new character which signs the multiplicator we reset our values
                    current = 0;
                    run = 1;
                }
            }
     
            return duration;
        }
     
        /**
         * Returns the number of a character. '9' will return the integer 9, '2' will return the integer 2
         *
         * @param character The character to parse
         * @return The integer which matches, or -1 if the parse fails
         */
        public static int getDigit(int character)
        {
            final int MINIMAL_DIGIT = 48, MAXIMAL_DIGIT = 57;
     
            if (character >= MINIMAL_DIGIT && character <= MAXIMAL_DIGIT) {
                return character - MINIMAL_DIGIT;
            }
     
            return -1;
        }
    50d21h5m55s will return 4399560000. Currently it supports only lower-case characters and it will break if there is any other character, which is not "parse-able".

    EDIT: Replaced the digit method. It's much faster now!

    p000ison
     
  2. Offline

    Acrobot

    Code:java
    1.  
    2. SimpleDateFormat dateFormat = new SimpleDateFormat("d'd'k'h'm'm's's'");
    3.  
    4. public static long parseTime(String time) {
    5. Date date = dateFormat.parse(time);
    6. return date.getTime();
    7. }
    8.  
    9.  


    This should also work.
     
    chaseoes likes this.
  3. Offline

    p000ison

    k true, but also about 15 times slower :p
     
    BananaBitchs likes this.
Thread Status:
Not open for further replies.

Share This Page