Java Minute Parse parseHHMMStringToMinutes(String time)

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

Description

Return the number of minutes for an hh:mm time value.

License

Apache License

Parameter

Parameter Description
time Time value (must not be null or blank string)

Exception

Parameter Description
IllegalArgumentException if the time value cannot be converted to minutes

Return

Time value converted to minutes

Declaration

public static int parseHHMMStringToMinutes(String time) 

Method Source Code

//package com.java2s;
/**//from  www . ja va 2s  . co  m
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

public class Main {
    private static final String COLON = ":";

    /**
     * Return the number of minutes for an hh:mm time value.
     * @param time Time value (must not be null or blank string)
     * @return Time value converted to minutes
     * @throws IllegalArgumentException if the time value cannot be converted to minutes
     */
    public static int parseHHMMStringToMinutes(String time) {
        String[] values = time.split(COLON);
        if (values.length == 2) {
            try {
                int hours = Integer.parseInt(values[0]);
                int minutes = Integer.parseInt(values[1]);
                return hours * 60 + minutes;
            } catch (NumberFormatException e) {
                // fall through to error.
            }
        }
        throw new IllegalArgumentException(
                "Invalid Time entry not HH:MM, was '" + time);
    }
}

Related

  1. parseHourAndMinute(String value)
  2. parseIntForMinute(Integer i)
  3. parseMinutes(long time)
  4. parseMinutes(String minutes, int defaultValue)