Java Number Convert convertDurationToMillis(String time)

Here you can find the source of convertDurationToMillis(String time)

Description

Calls the correct functions to convert the time period to milliseconds

License

Apache License

Parameter

Parameter Description
time a parameter

Return

milliseconds

Declaration

private static long convertDurationToMillis(String time) 

Method Source Code


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

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**//from w ww.ja v  a  2  s. c o m
     * Calls the correct functions to convert the time period to milliseconds
     * @param time
     * @return milliseconds
     */
    private static long convertDurationToMillis(String time) {

        if (time == null || (time != null && "".equals(time))) {
            return 0;
        }

        BigDecimal milliseconds = null;

        // String to be scanned to find the pattern.
        String line = time;
        String pattern = "(\\d+.\\d+|\\d+)(\\w+)";

        // Create a Pattern object
        Pattern r = Pattern.compile(pattern);

        // Now create matcher object.
        Matcher m = r.matcher(line);

        if (m.find()) {
            String duration = m.group(1);
            String timeScale = m.group(2).toLowerCase();

            switch (timeScale) {
            case "s":
                milliseconds = convertSecondsToMillis(new BigDecimal(duration));
                break;
            case "m":
                milliseconds = convertMinutesToMillis(new BigDecimal(duration));
                break;
            case "h":
                milliseconds = convertHoursToMillis(new BigDecimal(duration));
                break;
            case "d":
                milliseconds = convertDaysToMillis(new BigDecimal(duration));
                break;
            case "w":
                milliseconds = convertWeeksToMillis(new BigDecimal(duration));
                break;
            }
        }

        return milliseconds.longValueExact();
    }

    /**
     * Number of second = # of seconds *  1000 millis
     * @param seconds
     * @return seconds converted to milliseconds
     */
    private static BigDecimal convertSecondsToMillis(BigDecimal seconds) {
        return seconds.multiply(new BigDecimal(1000));
    }

    /**
     * Number of minutes = # of minutes * 60 seconds * 1000 millis
     * @param minutes
     * @return minutes converted to milliseconds
     */
    private static BigDecimal convertMinutesToMillis(BigDecimal minutes) {
        return minutes.multiply(convertSecondsToMillis(new BigDecimal(60)));
    }

    /**
     * Number of hours = # of hours * 60 seconds * 60 minutes * 1000 millis
     * @param hours
     * @return hours converted to milliseconds
     */
    private static BigDecimal convertHoursToMillis(BigDecimal hours) {
        return hours.multiply(convertMinutesToMillis(new BigDecimal(60)));
    }

    /**
     * Number of  days = Number of days * 24 hours * 60 seconds * 60 minutes * 1000 millis
     * @param days
     * @return days converted to milliseconds
     */
    private static BigDecimal convertDaysToMillis(BigDecimal days) {
        return days.multiply(convertHoursToMillis(new BigDecimal(24)));
    }

    /**
     * Number of weeks = # of weeks * 7 day * 24 hours * 60 seconds * 60 minutes * 1000 millis
     * @param weeks
     * @return weeks converted to milliseconds
     */
    private static BigDecimal convertWeeksToMillis(BigDecimal weeks) {
        return weeks.multiply(convertDaysToMillis(new BigDecimal(7)));
    }
}

Related

  1. convert(long value, long factor, int comma)
  2. convertBytesToKBytes(String value)
  3. convertBytesToUnit(long bytes)
  4. convertCount(Object obj)
  5. convertedFileSize(Long fileSize)
  6. convertFromMsToMinutes(Integer duration)
  7. convertGeoCoordinateToDouble(int point)
  8. convertImperialToMetric(String areaImp)