SimpleDateFormat

Discussion in 'Java' started by Eric, Sep 24, 2003.

  1. Eric

    Eric Guest

    I am having a heck of a time trying to parse 24 hour time format from String
    to a java.util.date

    I am trying to use SimpleDateFormat.getTimeInstance().parse("0800")

    but it throws a Parse Exception error.

    Can anyone give me some pointers or put me in the right direction?

    Thanks in advance for any help!!


    -Eric
     
    Eric, Sep 24, 2003
    #1
    1. Advertising

  2. Eric wrote:
    > I am having a heck of a time trying to parse 24 hour time format from String
    > to a java.util.date
    >
    > I am trying to use SimpleDateFormat.getTimeInstance().parse("0800")
    >
    > but it throws a Parse Exception error.
    >
    > Can anyone give me some pointers or put me in the right direction?


    Define your own SimpleDataFormat using a pattern that desribes the
    format you actually mean. The static method gives you "the default
    formatting style for the default locale", which in your case probably
    expects something like "08:00am".
     
    Michael Borgwardt, Sep 24, 2003
    #2
    1. Advertising

  3. Eric

    Roedy Green Guest

    On Wed, 24 Sep 2003 14:07:12 GMT, "Eric" <> wrote
    or quoted :

    >I am trying to use SimpleDateFormat.getTimeInstance().parse("0800")


    what mask did you set up for your SimpleDateFormat?

    See http://mindprod.com/jgloss/calendar.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 24, 2003
    #3
  4. Eric

    Todd Shillam Guest

    Eric,

    Here's a few snippets of code to that may shed some light on how to use
    Java's SimpleDateFormat.


    //CREATING SIMPLE_DATE_FORMAT TO DISPLAY MONTH/DAY/YEAR
    SimpleDateFormat formatter;
    formatter = new SimpleDateFormat("MM/dd/yyyy", Locale.US);

    //NEW DATE OBJECT--INITIALZIED WITH CURRENT SYSTEM DATE
    today = new Date();

    //CONVERTS DATE (TODAY) INTO STRING FORMAT
    output = formatter.format(today);

    //PRINTS STRING VARIABLE
    System.out.println("Current Date (MM/DD/YYYY): " + output);
    System.out.println();

    //COLLECTING USER INPUT FOR DATE (MM/DD/YYYY)
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println();
    System.out.print("Please enter new date (MM/DD/YYYY): ");
    String userInput = br.readLine();

    //INPUT VARIABLE PARSED INTO A DATE--INITIALIZES
    today = formatter.parse(userInput);

    //DATES CONVERTED BACK INTO A STRING
    output = formatter.format(today);

    //STRING DISPLAYED
    System.out.println(output);

    Good luck,
    --
    Todd


    "Eric" <> wrote in message
    news:kyhcb.156899$...
    I am having a heck of a time trying to parse 24 hour time format from String
    to a java.util.date

    I am trying to use SimpleDateFormat.getTimeInstance().parse("0800")

    but it throws a Parse Exception error.

    Can anyone give me some pointers or put me in the right direction?

    Thanks in advance for any help!!


    -Eric
     
    Todd Shillam, Sep 24, 2003
    #4
  5. Eric

    Chris Smith Guest

    Eric wrote:
    > I am having a heck of a time trying to parse 24 hour time format from String
    > to a java.util.date
    >
    > I am trying to use SimpleDateFormat.getTimeInstance().parse("0800")


    The code above is actually identical to DateFormat.getTimeInstance(),
    and has nothing to do with SimpleDateFormat. It's unfortunate that the
    Java language allows this kind of error. In any case, you use
    SimpleDateFormat by creating an instance, not calling static methods.
    The format mask is specified in the API docs, but in your case it would
    be:

    new SimpleDateFormat("kkmm").parse("0800");

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Sep 25, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manuel
    Replies:
    0
    Views:
    557
    Manuel
    Aug 28, 2003
  2. Niko
    Replies:
    10
    Views:
    2,471
    Wojtek
    Sep 18, 2003
  3. Bill Angel
    Replies:
    4
    Views:
    1,359
    Bill Angel
    Sep 28, 2003
  4. Kyote
    Replies:
    16
    Views:
    4,180
    Michael Borgwardt
    Oct 24, 2003
  5. Christopher Dean

    Synchronizing on SimpleDateFormat vs. clone

    Christopher Dean, Nov 18, 2003, in forum: Java
    Replies:
    3
    Views:
    862
    Christopher Dean
    Nov 25, 2003
Loading...

Share This Page