Java Parse Time parseTime(String sTime)

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

Description

Parse the given time ( supposed to be in ISO format : "HH:MM:SS" )

License

LGPL

Parameter

Parameter Description
sTime a parameter

Exception

Parameter Description
TelosysRuntimeException if the time is invalid

Return

time or null if the given string is null or void

Declaration

public static java.util.Date parseTime(String sTime) 

Method Source Code

//package com.java2s;
/**//from   w  w  w .  j  a  v a2s.  co  m
 *  Copyright (C) 2008-2014  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.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.gnu.org/licenses/lgpl.html
 *
 *  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.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Main {
    private final static String TIME_ISO_FORMAT = "HH:mm:ss";
    private final static String INVALID_TIME_FORMAT = "invalid format 'HH:MM:SS' expected";

    /**
     * Parse the given time ( supposed to be in ISO format : "HH:MM:SS" )
     * @param sTime
     * @return time or null if the given string is null or void
     * @throws TelosysRuntimeException if the time is invalid
     * @since 1.0.2
     */
    public static java.util.Date parseTime(String sTime) {
        if (sTime == null)
            return null;
        if (sTime.length() == 0)
            return null;

        char c = 0;
        for (int i = 0; i < 8; i++) // the length is 8 "HH:MM:SS"
        {
            c = sTime.charAt(i);
            if ((c < '0' || c > '9') && (c != ':')) {
                throwParseTimeException(sTime, INVALID_TIME_FORMAT);
            }
            if (c == ':' && (i != 2 && i != 5)) {
                throwParseTimeException(sTime, INVALID_TIME_FORMAT);
            }
        }

        java.util.Date ret = null;
        try {
            //--- Try to parse the input date ( with non lenient parsing => check validity )
            //            TIME_FORMAT_ISO.setLenient(false); // non lenient parsing ( exception if invalid date )
            //            ret = TIME_FORMAT_ISO.parse(sTime);

            SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_ISO_FORMAT);
            dateFormat.setLenient(false); // non lenient parsing ( exception if invalid date )
            ret = dateFormat.parse(sTime);

        } catch (ParseException e) {
            throwParseTimeException(sTime, "invalid time");
        }
        return ret;
    }

    private static void throwParseTimeException(String sTime, String sMsg) {
        throw new RuntimeException("Cannot parse time '" + sTime + "' : " + sMsg);
    }
}

Related

  1. parseTime(char[] timeChars)
  2. ParseTime(final String time)
  3. parseTime(String currDate, String format)
  4. parseTime(String date, String format)
  5. parseTime(String s)
  6. parseTime(String str)
  7. parseTime(String str)
  8. parseTime(String t)
  9. parseTime(String text)