The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.text  [26 examples] > Times  [4 examples]

e317. Parsing the Time Using a Custom Format

A pattern of special characters is used to specify the format of the time to parse. The same set of pattern characters are used to parse times. See e316 Formatting the Time Using a Custom Format for a listing of some pattern characters.

Note: This example formats dates using the default locale (which, in the author's case, is Locale.ENGLISH). If the example is run in a different locale, the text (e.g., month names) will not be the same.

    try {
        // Some examples
        DateFormat formatter = new SimpleDateFormat("hh.mm.ss a");
        Date date = (Date)formatter.parse("02.36.33 PM");
    
        formatter = new SimpleDateFormat("HH:mm:ss Z");
        date = (Date)formatter.parse("22:14:02 -0500");
    } catch (ParseException e) {
    }

 Related Examples
e316. Formatting the Time Using a Custom Format
e318. Formatting and Parsing a Time for a Locale Using Default Formats
e319. Formatting and Parsing a Time for a Locale

See also: Dates    Messsages    Numbers    Words and Sentences   


© 2002 Addison-Wesley.