Java Parse Time parseTime(String str)

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

Description

Parses time of day in hh:mm aa format.

License

Open Source License

Parameter

Parameter Description
str String.

Return

The time, or null if syntax error.

First attempts to parse the time in "hh:mm aa" format. If that fails, tries "hh:mm". The latter allows for inputting the time on a 24 clock basis.

Declaration


static public Date parseTime(String str) 

Method Source Code


//package com.java2s;
/*   Please see the license information at the end of this file. */

import java.util.*;

import java.text.*;

public class Main {
    /**   Timer formatter for dates in format hh:mm aa. */

    static private final SimpleDateFormat timeFormatter = new SimpleDateFormat("hh:mm aa");
    /**   Timer formatter for dates in format hh:mm . */

    static private final SimpleDateFormat timeFormatter2 = new SimpleDateFormat("hh:mm");

    /**   Parses time of day in hh:mm aa format.
     *//  www. ja  va2s . co  m
     *   @param   str         String.
     *
     *   @return            The time, or null if syntax error.
     *
     *   <p>
     *   First attempts to parse the time in "hh:mm aa" format.
     *   If that fails, tries "hh:mm".  The latter allows for
     *   inputting the time on a 24 clock basis.
     *   </p>
     */

    static public Date parseTime(String str) {
        ParsePosition pos = new ParsePosition(0);

        Date result = timeFormatter.parse(str, pos);

        if ((result != null) && (pos.getIndex() < str.length()))
            result = null;

        if (result == null) {
            pos = new ParsePosition(0);

            result = timeFormatter2.parse(str, pos);

            if ((result != null) && (pos.getIndex() < str.length()))
                result = null;
        }

        return result;
    }
}

Related

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