Results 1 to 2 of 2
  1. #1
    plasma is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default Convert String to Time format

    Hi there. I am new to java and am currently facing a problem with converting a string to time format. Here is my code:

    String str = "09:00:00 EST" ;
    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss z");
    System.out.println(str);
    try {
    Date df = dateFormat.parse(str);
    System.out.println(df);
    } catch (Exception e) {
    System.out.println(e.getMessage)
    }

    Output:
    09:00:00 EST
    Thu Jan 01 19:30:00 IST 1970

    I am confused as to how the wrong time is got in the output. Please help me out. Thanks in advance.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    The time is not wrong. It is being output in the local timezone and the "toString" format, as outlined in the API docs. If you want to output the time in a different format and timezone, then you need to set the timezone on the SimpleDateFormat object and output the Date string using SimpleDateFormat and its format method.

Similar Threads

  1. How Convert a Byte array to a image format
    By perlWhite in forum New To Java
    Replies: 2
    Last Post: 08-23-2009, 09:11 PM
  2. how to convert SWING GUI into exe format
    By santhosh_el in forum AWT / Swing
    Replies: 7
    Last Post: 04-04-2009, 01:36 PM
  3. how to convert one format to another format
    By mahipal_reddy621 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 11:21 AM
  4. validate time input format
    By Alairmon in forum New To Java
    Replies: 2
    Last Post: 11-04-2008, 08:39 AM
  5. how to convert date format
    By saran123 in forum New To Java
    Replies: 5
    Last Post: 10-16-2008, 07:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •