Java Milliseconds getMillis(String decimal)

Here you can find the source of getMillis(String decimal)

Description

Gets the number of millis represented by a String containing a decimal(8,3) type.

License

Open Source License

Declaration

public static int getMillis(String decimal) 

Method Source Code

//package com.java2s;
/*/*from   w w w .j a  v  a  2  s.c  o  m*/
 * aocode-public - Reusable Java library of general tools with minimal external dependencies.
 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013, 2016, 2018  AO Industries, Inc.
 *     support@aoindustries.com
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of aocode-public.
 *
 * aocode-public is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aocode-public is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with aocode-public.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Gets the number of millis represented by a <code>String</code> containing a decimal(8,3) type.
     */
    public static int getMillis(String decimal) {
        // Get the sign first, treat as negative, then apply the sign
        boolean isNegative;
        if (decimal.length() > 0 && decimal.charAt(0) == '-') {
            isNegative = true;
            decimal = decimal.substring(1);
        } else
            isNegative = false;

        // Add zero to beginning if starts with .
        if (decimal.length() > 0 && decimal.charAt(0) == '.')
            decimal = '0' + decimal;

        // Allow for incomplete data like 2, 2., 2.3, and 2.34
        if (decimal.indexOf('.') == -1)
            decimal += ".000";
        else if (decimal.charAt(decimal.length() - 1) == '.')
            decimal += "000";
        else if (decimal.length() >= 2 && decimal.charAt(decimal.length() - 2) == '.')
            decimal += "00";
        else if (decimal.length() >= 3 && decimal.charAt(decimal.length() - 3) == '.')
            decimal += '0';

        int len = decimal.length();
        int whole = Integer.parseInt(decimal.substring(0, len - 4));
        int millis = Integer.parseInt(decimal.substring(len - 3));
        long result = (isNegative ? -1L : 1L) * (whole * 1000L + millis);
        if (result < Integer.MIN_VALUE || result > Integer.MAX_VALUE)
            throw new NumberFormatException("Out of range during conversion");
        return (int) result;
    }
}

Related

  1. getMillis()
  2. getMillis(long ticks)
  3. getMillis(long timeInMinutes)
  4. getMillis(String _cal)
  5. getMillis(String dateStr)
  6. getMillisDisplayable(long millis)
  7. getMilliSecond(Date d1, Date d2)
  8. getMillisecond(Date date)
  9. getMillisecond(String forDate)