Java SQL Time Parse parseTime(String timeString)

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

Description

parseTime: Returns a Time SQL object corresponding to the given time string.

License

Open Source License

Parameter

Parameter Description
timeString a parameter

Return

Time

Declaration

public static Time parseTime(String timeString) 

Method Source Code

//package com.java2s;

import java.sql.Time;

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

import java.util.Date;

public class Main {
    /**//from www . j  av a 2s.c  o m
     * parseTime: Returns a Time SQL object corresponding to the given time string.  Date portion returned is 1/1/1970
     * so the returned Time is only meant to be treated as a Time object.
     * @param timeString
     * @return Time
     */
    public static Time parseTime(String timeString) {
        // Try basic format (e.g. "2:00 AM")
        Date javaDate = null;
        try {
            javaDate = new SimpleDateFormat("hh:mm aaa").parse(timeString);
        } catch (ParseException e) {
        }

        // Try another format if no result yet (e.g. "2 AM")
        if (javaDate == null) {
            try {
                javaDate = new SimpleDateFormat("hh aaa").parse(timeString);
            } catch (ParseException e) {
            }
        }

        // Try another format if no result yet (e.g. "0200")
        if (javaDate == null) {
            try {
                javaDate = new SimpleDateFormat("HHss").parse(timeString);
            } catch (ParseException e) {
            }
        }

        if (javaDate == null)
            throw new IllegalArgumentException("Cannot recognize input as time format: " + timeString);

        return new Time(javaDate.getTime());

    }
}

Related

  1. parseDateTime(String original, int type)
  2. parseTime(String s)
  3. parseTime(String str)
  4. printTime(Time time, String format)
  5. string2Time(String dateString)
  6. string2Time(String time, DateFormat timeFormat)
  7. stringToTime(String data, String dateFormat)