Java Milliseconds Parse parseTimeSpanMillis(final String text)

Here you can find the source of parseTimeSpanMillis(final String text)

Description

Parse a time span expression and returns the milliseconds represented by this one.

License

Apache License

Parameter

Parameter Description
text the span expression

Return

the number of milliseconds

Declaration

public static Long parseTimeSpanMillis(final String text) 

Method Source Code

//package com.java2s;
/*// w  ww  .j  a  va  2s. co  m
 * Copyright (c) 2016, The Dattack team (http://www.dattack.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    private static final long MILLIS_PER_SECOND = 1000;
    private static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
    private static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
    private static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;
    private static final long MILLIS_PER_WEEK = 7 * MILLIS_PER_DAY;

    /**
     * <p>
     * Parse a time span expression and returns the milliseconds represented by this one. A valid time span expression
     * has the following format: <code>(&lt;digit+&gt;&lt;letter&gt;)+</code>. The following pattern letters are
     * defined:
     * </p>
     *
     * <table summary="">
     * <thead>
     * <tr>
     * <td>Letter</td>
     * <td>Time unit</td>
     * </tr>
     * </thead> <tbody>
     * <tr>
     * <td>w or W</td>
     * <td>Week</td>
     * </tr>
     * <tr>
     * <td>d or D</td>
     * <td>Day</td>
     * </tr>
     * <tr>
     * <td>h or H</td>
     * <td>Hour</td>
     * </tr>
     * <tr>
     * <td>m or M</td>
     * <td>Minute</td>
     * </tr>
     * <tr>
     * <td>s or S</td>
     * <td>Second</td>
     * </tr>
     * </tbody>
     * </table>
     *
     * <p>
     * Example: <code>2h30m = 2 hours + 30 minutes = 2 * 60 * 60 * 1000 + 30 * 60 * 1000 = 9.000.000 milliseconds</code>
     * </p>
     *
     * @param text
     *            the span expression
     * @return the number of milliseconds
     */
    public static Long parseTimeSpanMillis(final String text) {

        Long result = null;
        if (text != null) {
            try {
                result = Long.valueOf(text);
            } catch (@SuppressWarnings("unused") final NumberFormatException e) {
                result = parseTimeSpanExpression(text);
            }
        }
        return result;
    }

    /**
     * Computes the number of milliseconds represented by the specified span expression.
     *
     * @param text
     *            the span expression
     * @return the number of milliseconds
     */
    private static Long parseTimeSpanExpression(final String text) {

        long timeInMillis = 0;
        long value = 0;
        for (int i = 0; i < text.length(); i++) {
            final char charAt = text.charAt(i);
            if (Character.isDigit(charAt)) {
                value = value * 10 + Character.digit(charAt, 10);
            } else {
                switch (charAt) {
                case 'w':
                case 'W':
                    timeInMillis += value * MILLIS_PER_WEEK;
                    break;
                case 'd':
                case 'D':
                    timeInMillis += value * MILLIS_PER_DAY;
                    break;
                case 'h':
                case 'H':
                    timeInMillis += value * MILLIS_PER_HOUR;
                    break;
                case 'm':
                case 'M':
                    timeInMillis += value * MILLIS_PER_MINUTE;
                    break;
                case 's':
                case 'S':
                    timeInMillis += value * MILLIS_PER_SECOND;
                    break;
                default:
                    throw new IllegalArgumentException(String.format("Unknown time unit: '%s'", charAt));
                    // ignore value
                }
                value = 0;
            }
        }
        timeInMillis += value;
        return timeInMillis;
    }
}

Related

  1. getTimeMillis(String time)
  2. parseDate(long millisec)
  3. parseGMTInMillis(String time)
  4. parseMillis(String s)
  5. parseMillisecond(String date)
  6. parseTimeStrToMilliseconds(String timeStr)
  7. parseToMillis(String s)
  8. pauseMilliseconds(long duration)
  9. pauseMilliSeconds(long pauseMilliSeconds)