Java Scanner Usage parseTime(String input)

Here you can find the source of parseTime(String input)

Description

parse Time

License

Open Source License

Declaration

public static long parseTime(String input) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Locale;
import java.util.Scanner;

public class Main {
    public static long parseTime(String input) {
        Scanner scanner = new Scanner(input.trim());
        scanner.useLocale(Locale.ENGLISH);

        long time;

        // 00:00.000
        if (input.contains(":")) {
            scanner.useDelimiter(":");

            if (!scanner.hasNextLong()) {
                return 0;
            }//  www . j  av  a 2 s . co  m

            long minutes = scanner.nextLong();
            if (minutes < 0) {
                return 0;
            }

            if (!scanner.hasNextDouble()) {
                return 0;
            }

            double seconds = scanner.nextDouble();
            if (seconds < 0.0 || seconds >= 60.0) {
                return 0;
            }

            time = (long) (60000 * minutes + 1000 * seconds);
        }

        // 00.000
        else {
            if (!scanner.hasNextDouble()) {
                return 0;
            }

            double seconds = scanner.nextDouble();
            if (seconds < 0.0) {
                return 0;
            }

            time = (long) (1000 * seconds);
        }

        return time;
    }
}

Related

  1. parameterInput(int start, int end)
  2. parse_str_vector(String str_vector)
  3. parseDailyData(final List lines, final List priceValues, final List dailyPriceDate)
  4. parseExperimentName(String name)
  5. parseQueryString(String queryString)
  6. pathTokenList(String path)
  7. pressEnterToContinue()
  8. processLine(String aLine)
  9. queryMenu(Scanner in, String msg, LinkedList options)