Java String to Time toTime(String str)

Here you can find the source of toTime(String str)

Description

Parses a String as an SQL time.

License

Open Source License

Declaration

public static Date toTime(String str) 

Method Source Code

//package com.java2s;
/**//  w  w  w  .  j  a v  a  2 s . c  om
 * com.mckoi.database.global.CastHelper  11 Oct 2001
 *
 * Mckoi SQL Database ( http://www.mckoi.com/database )
 * Copyright (C) 2000, 2001, 2002  Diehl and Associates, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * Version 2 as published by the Free Software Foundation.
 *
 * This program 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 General Public License Version 2 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * Version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Change Log:
 * 
 * 
 */

import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.text.DateFormat;
import java.util.Date;

public class Main {
    private static DateFormat[] time_format_sql;

    /**
     * Parses a String as an SQL time.
     */
    public static Date toTime(String str) {
        synchronized (time_format_sql) {
            for (int i = 0; i < time_format_sql.length; ++i) {
                try {
                    return time_format_sql[i].parse(str);
                } catch (ParseException e) {
                }
            }
            throw new RuntimeException(dateErrorString("Unable to parse string as a time ", time_format_sql));
        }
    }

    /**
     * Helper that generates an appropriate error message for a date format error.
     */
    private static String dateErrorString(String msg, DateFormat[] df) {
        String pattern = "";
        if (df[0] instanceof SimpleDateFormat) {
            SimpleDateFormat sdf = (SimpleDateFormat) df[0];
            pattern = "(" + sdf.toPattern() + ")";
        }
        return msg + pattern;
    }
}

Related

  1. stringToCalendar(String stringTime)
  2. stringToCalendar(String timeStr)
  3. stringToTimeDate(String timeStr)
  4. toTime(final String dateString)
  5. toTime(String display)