Java Milliseconds Parse parseMillis(String s)

Here you can find the source of parseMillis(String s)

Description

Parses a formatted String and returns the value in milliseconds.

License

Apache License

Declaration

public static long parseMillis(String s) 

Method Source Code

//package com.java2s;
/* 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.//  w ww. j a va2  s.  c o m
 */

public class Main {
    /**
     * Parses a formatted String and returns the value in milliseconds. You can
     * use one of the following suffixes:
     *
     * <pre>
     *     s - seconds
     *     m - minutes
     *     h - hours
     *     D - days
     *     W - weeks
     *     M - months
     *     Y - years
     * </pre>
     */
    public static long parseMillis(String s) {
        if (s == null) {
            return 0;
        }
        long millis = 0;
        int i = 0;
        int length = s.length();
        while (i < length) {
            long delta = 0;
            char ch = 0;
            for (; i < length; i++) {
                ch = s.charAt(i);
                if (!Character.isDigit(ch)) {
                    i++;
                    break;
                }
                delta *= 10;
                delta += Character.getNumericValue(ch);
            }
            switch (ch) {
            case 's':
            case 'S':
            default:
                millis += 1000 * delta;
                break;

            case 'm':
                millis += 60 * 1000 * delta;
                break;

            case 'h':
            case 'H':
                millis += 60L * 60 * 1000 * delta;
                break;

            case 'd':
            case 'D':
                millis += 24L * 60 * 60 * 1000 * delta;
                break;

            case 'w':
            case 'W':
                millis += 7L * 24 * 60 * 60 * 1000 * delta;
                break;

            case 'M':
                millis += 30L * 24 * 60 * 60 * 1000 * delta;
                break;

            case 'y':
            case 'Y':
                millis += 365L * 24 * 60 * 60 * 1000 * delta;
                break;
            }
        }
        return millis;
    }
}

Related

  1. getTimeInMilliseconds(String timeString)
  2. getTimeMillis(long FileTimestamp)
  3. getTimeMillis(String time)
  4. parseDate(long millisec)
  5. parseGMTInMillis(String time)
  6. parseMillisecond(String date)
  7. parseTimeSpanMillis(final String text)
  8. parseTimeStrToMilliseconds(String timeStr)
  9. parseToMillis(String s)