Java Scanner Usage getTimeAsInt(String duration)

Here you can find the source of getTimeAsInt(String duration)

Description

get Time As Int

License

Open Source License

Declaration

public static int getTimeAsInt(String duration) 

Method Source Code

//package com.java2s;
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 *
 *  Sun Microsystems Inc., March, 2001//from  w ww . j  a  va  2  s  .  co m
 *
 *
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *   Copyright: 2001 by Sun Microsystems, Inc.
 *
 *   All Rights Reserved.
 *
 ************************************************************************/

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

public class Main {
    public static final String PATTERN_TIME = "\\d*:?\\d*:?\\d*";

    public static int getTimeAsInt(String duration) {
        if (!isValueValid(duration, PATTERN_TIME) || duration.length() == 0) {
            throw new IllegalArgumentException(duration + " is not a time type");
        }
        if (duration.endsWith(":")) {
            duration += " ";
        }
        Scanner s = new Scanner(duration);
        int res = 0;
        s.useLocale(Locale.US);//TODO LP: What to do with locale formatted numbers?
        s.useDelimiter(":");
        String val;
        while (s.hasNext()) {
            res = res * 60;
            if (s.hasNextInt()) {
                res += s.nextInt();
            } else {
                s.next();
            }
        }
        s.close();
        return res;
    }

    private static boolean isValueValid(String value, String pattern) {
        Scanner s = new Scanner(value);
        try {
            s.next("^(" + pattern + ")$");
            return true;
        } catch (InputMismatchException ex) {
            return false;
        }
    }
}

Related

  1. getPartOfString(String string, int index, String delimiter)
  2. getReplay(String prompt)
  3. getScannerContents(final Scanner scanner)
  4. getStrings(Properties props, String key, String sep)
  5. getStrings(Scanner sc, int n)
  6. getTypedValue(String question)
  7. input(String message)
  8. input(String prompt)
  9. isInteger(String s)