Java Parse Time parseTime(String t)

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

Description

Parse a time string in one of the supported formats.

License

Open Source License

Parameter

Parameter Description
t Time string.

Return

Date object, or null if time could not be parsed.

Declaration

static public Date parseTime(String t) 

Method Source Code

//package com.java2s;
/*/*from ww w .j  a v a 2s  .c  o  m*/
 * IRIS -- Intelligent Roadway Information System
 * Copyright (C) 2009-2014  Minnesota Department of Transportation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 for more details.
 */

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

import java.util.Date;

public class Main {
    /** Time parser formats */
    static private final DateFormat[] TIME_FORMATS = {
            new SimpleDateFormat("h:mm a"), new SimpleDateFormat("H:mm"),
            new SimpleDateFormat("h a"), new SimpleDateFormat("H") };

    /** Parse a time string in one of the supported formats.
     * @param t Time string.
     * @return Date object, or null if time could not be parsed. */
    static public Date parseTime(String t) {
        for (DateFormat df : TIME_FORMATS) {
            try {
                return df.parse(t);
            } catch (ParseException e) { /* ignore */
            } catch (NumberFormatException e) { /* ignore */
            }
        }
        return null;
    }
}

Related

  1. parseTime(String date, String format)
  2. parseTime(String s)
  3. parseTime(String sTime)
  4. parseTime(String str)
  5. parseTime(String str)
  6. parseTime(String text)
  7. parseTime(String time)
  8. parseTime(String time)
  9. parseTime(String time, String formatStrBefore, String formatStrAfter)