Java Parse Time parseTime(String str)

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

Description

Parse the time from a string and return it.

License

Open Source License

Return

an initialized object or null if it can't parse the time. (It does not throw ParseException)

Declaration

static public Date parseTime(String str) 

Method Source Code


//package com.java2s;
/* The DateUtil utility provides several workhorse methods that
 * use Java's formatters to parse and format dates, times, etc.
 *
 * <!-- ====================== distribution terms ===================== -->
 * <p><blockquote// w  w w .j a v  a 2s . co m
 *    style="border-style: solid; border-width:thin; padding: 1em 1em 1em 1em;">
 * <center>
 *          Copyright &copy; 2003, Allen I. Holub. All rights reserved.
 * </center>
 * <br>
 * <br>
 *       This code is distributed under the terms of the
 *       <a href="http://www.gnu.org/licenses/gpl.html"
 *       >GNU Public License</a> (GPL)
 *       with the following ammendment to section 2.c:
 *       <p>
 *       As a requirement for distributing this code, your splash screen,
 *       about box, or equivalent must include an my name, copyright,
 *       <em>and URL</em>. An acceptable message would be:
 * <center>
 *       This program contains Allen Holub's <em>XXX</em> utility.<br>
 *             (c) 2003 Allen I. Holub. All Rights Reserved.<br>
 *                   http://www.holub.com<br>
 * </center>
 *       If your progam does not run interactively, then the foregoing
 *       notice must appear in your documentation.
 * </blockquote>
 * <!-- =============================================================== -->
 * @author Allen I. Holub
 */

import java.text.*;
import java.util.*;

public class Main {
    /** Parse the time from a string and return it. This method tries
     *  all the formats supported by {@link DateFormat} (FULL, LONG,
     *  MEDIUM, and SHORT in that order).
     *  @return an initialized {@link java.util.Date} object or
     *  <code>null</code> if it can't parse the time.
     *  (It does not throw <code>ParseException</code>)
     */
    static public Date parseTime(String str) {
        // assert str != null;

        Date date = null;
        try {
            date = DateFormat.getTimeInstance(DateFormat.FULL).parse(str);
        } catch (ParseException pel) {
            try {
                date = DateFormat.getTimeInstance(DateFormat.LONG).parse(str);
            } catch (ParseException pe2) {
                try {
                    date = DateFormat.getTimeInstance(DateFormat.MEDIUM).parse(str);
                } catch (ParseException pe3) {
                    try {
                        date = DateFormat.getTimeInstance(DateFormat.SHORT).parse(str);
                    } catch (ParseException pe4) {
                    }
                }
            }
        }
        return date;
    }
}

Related

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