Java Milliseconds timespanToMillis(String timeString)

Here you can find the source of timespanToMillis(String timeString)

Description

timespan To Millis

License

Apache License

Declaration

public static long timespanToMillis(String timeString) throws ParseException 

Method Source Code


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

import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static long timespanToMillis(String timeString) throws ParseException {
        Pattern pattern = Pattern.compile("(-?\\d+\\.?\\d*)([\\w]{0,1})", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(timeString);
        if (matcher.matches()) {
            double baseMillis = Double.parseDouble(matcher.group(1));
            String unit = matcher.group(2).toLowerCase();
            if (unit.equals("d") || unit.length() == 0) {
                return (long) (baseMillis * 1000 * 3600 * 24);
            } else if (unit.equals("h")) {
                return (long) (baseMillis * 1000 * 3600);
            } else if (unit.equals("w")) {
                return (long) (baseMillis * 1000 * 3600 * 24 * 7);
            } else if (unit.equals("m")) {
                return (long) (baseMillis * 1000 * 3600 * 24 * 30);
            } else if (unit.equals("y")) {
                return (long) (baseMillis * 1000 * 3600 * 24 * 360);
            }/* ww  w  .  j a v a  2 s . com*/
        }
        throw new ParseException(timeString, 0);
    }
}

Related

  1. timeMillisToString(long timeMillis)
  2. timeoutPropertyToIntMillis(long value)
  3. timeSince_MILLIS(long start_PMILLIS)
  4. timeSinceMillis(long prevTime)
  5. timeSinceMillis(long timestart)
  6. timestampToString(long lastModifiedMillis)
  7. timeToMilliseconds(Object unit, long interval)
  8. timeToString(long timeMillis)
  9. timeToTag(long milliSeconds)