Problems Parsing Date string with SimpleDateFormat

Discussion in 'Java' started by Bill Angel, Sep 27, 2003.

  1. Bill Angel

    Bill Angel Guest

    Hi:

    I am having problems parsing a date
    Here is a small example that illustrates the problem:

    package virusAnalyzer;
    import java.io.*;
    import java.text.*;
    import java.util.*;

    public class dateFormatTest {
    public static void main(String[] args) {
    try {

    SimpleDateFormat sdf =
    new SimpleDateFormat("MMM DD hh:mm:ss yyyy",Locale.US);

    Date messageDate = sdf.parse("Sep 25 21:52:14 2003");

    Calendar mycal = Calendar.getInstance();

    mycal.setTime(messageDate);

    System.out.println("messageDate= " + mycal.getTime());
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    The following line is output by the program:

    messageDate= Sat Jan 25 21:52:14 EST 2003

    NOTE: The date has been parsed so that the month of Jan has been
    output, which is incorrect. The month of Sep should
    have been written out.

    I ran this program with both JDK 1.2.2 and JDK 1.3.1
    and got the same result.

    Is there something wrong with this code?

    Thanks,

    Bill Angel
     
    Bill Angel, Sep 27, 2003
    #1
    1. Advertising

  2. Bill Angel

    Marko Lahma Guest

    > SimpleDateFormat sdf =
    > new SimpleDateFormat("MMM DD hh:mm:ss yyyy",Locale.US);
    >
    > Date messageDate = sdf.parse("Sep 25 21:52:14 2003");


    DD means day of year so 25 would hit quite nicely to January, use dd
    (day in month) instead of DD. Second error is that it should be HH for
    00-23 hours (hh is 0-12 am/pm).

    -Marko
     
    Marko Lahma, Sep 28, 2003
    #2
    1. Advertising

  3. Bill Angel

    Bill Angel Guest

    "Marko Lahma" <marko@#NOSPAM#lahma.net> wrote in message news:bl554v$im$...
    > > SimpleDateFormat sdf =
    > > new SimpleDateFormat("MMM DD hh:mm:ss yyyy",Locale.US);
    > >
    > > Date messageDate = sdf.parse("Sep 25 21:52:14 2003");

    >
    > DD means day of year so 25 would hit quite nicely to January, use dd
    > (day in month) instead of DD. Second error is that it should be HH for
    > 00-23 hours (hh is 0-12 am/pm).
    >
    > -Marko
    >


    Thank you very much!
    That solved that problem...

    Now I've got a slightly different problem...
    I've added a statement to the program to return the Month from the Calendar object:

    package virusAnalyzer;
    import java.io.*;
    import java.text.*;
    import java.util.*;
    public class dateFormatTest {
    public static void main(String[] args) {
    try {
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd HH:mm:ss yyyy",Locale.US);
    Date messageDate = sdf.parse("Sep 25 21:52:14 2003");
    Calendar mycal = Calendar.getInstance();
    mycal.setTime(messageDate);
    System.out.println("messageDate= "+ mycal.getTime() + " Month in Year = " + mycal.MONTH);
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    The program returned the following output:

    messageDate= Thu Sep 25 21:52:14 EDT 2003 Month in Year = 2

    Now the date returned is fine, but the Month in year value is 2. The value should have been 8.

    Any suggestions as to what the problem is?
    What I want to be able to do is to determine the month from the value of a date that the program will be reading
    in the specified format from an input file.

    Thanks,

    --- Bill Angel
     
    Bill Angel, Sep 28, 2003
    #3
  4. Bill Angel

    Marko Lahma Guest

    > System.out.println("messageDate= "+ mycal.getTime() + " Month in Year =
    > " + mycal.MONTH);
    > The program returned the following output:
    > messageDate= Thu Sep 25 21:52:14 EDT 2003 Month in Year = 2
    > Now the date returned is fine, but the Month in year value is 2. The
    > value should have been 8.


    You really should read the API documentation. Calendar.MONTH (also
    available in instance) is a key for a field. You can retrieve the month
    from calendar object with int month = mycal.get(Calendar.MONTH); (or
    get(mycal.MONTH) but that is just ugly)

    See
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/GregorianCalendar.html
    for documentation.

    -Marko
     
    Marko Lahma, Sep 28, 2003
    #4
  5. Bill Angel

    Bill Angel Guest

    "Marko Lahma" <marko@#NOSPAM#lahma.net> wrote in message news:bl6jfh$itj$...
    > > System.out.println("messageDate= "+ mycal.getTime() + " Month in Year =
    > > " + mycal.MONTH);
    > > The program returned the following output:
    > > messageDate= Thu Sep 25 21:52:14 EDT 2003 Month in Year = 2
    > > Now the date returned is fine, but the Month in year value is 2. The
    > > value should have been 8.

    >
    > You really should read the API documentation. Calendar.MONTH (also
    > available in instance) is a key for a field. You can retrieve the month
    > from calendar object with int month = mycal.get(Calendar.MONTH); (or
    > get(mycal.MONTH) but that is just ugly)
    >
    > See
    > http://java.sun.com/j2se/1.4.2/docs/api/java/util/GregorianCalendar.html
    > for documentation.
    >
    > -Marko
    >


    Thanks for pointing out my oversight in how to retrieve the month from
    a Calendar object. I feel a need to reply, however, to your statement
    describing the use of the statement int month = mycal.get(Calendar.MONTH);
    as "ugly"

    What I am utilizing this function for is determining how to process log messages
    based on the log message's date. I'm filtering the messages based
    on the day, month, and hour that the message arrived. The following code shows how
    this is being done:


    // An Hourly Summary Object collects aggregate information
    // for all events posted to the log for a one hour period

    // Create an array to hold all of the Hourly Summary Objects
    // 365 = days in year, 31 = days in month, 24 = hours in day

    HourlySummary resultsArray[][][] = new HourlySummary[365][31][24];

    // specify Date Formatter to parse LogFile Message's date

    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd HH:mm:ss yyyy",Locale.US);
    Calendar mycal = new GregorianCalendar();

    // process all the entries in the logfile

    while (true) {

    // read a line from the log file
    String line = source.readLine();

    // if no more lines in file then exit from loop

    if( line == null) break;

    // extract the dateTime Field from the log entry

    String dateText = line.substring(10,15);

    // convert dateText to Date Object

    Date messageDate = sdf.parse(dateText);

    // Initialize the Gregorian Calendar Object with extracted date

    mycal.setTime(messageDate);

    // determine indexes for the day of the Month, the hour of the Day, and the month

    int dayOfMonth = mycal.get(Calendar.DAY_OF_MONTH);
    int hourOfDay = mycal.get(Calendar.HOUR_OF_DAY);
    int month = mycal.get(Calendar.MONTH);

    // if HourlySummary Object for this day, hour, and month does not yet exist, then create one

    if (resultsArray[month][dayOfMonth][hourOfDay] == null)
    resultsArray[month][dayOfMonth][hourOfDay] = new HourlySummary();

    // continue processing the logfile by updating the aggregate results contained in
    // this HourlySummary object with information extracted from the logfile entry
    // for this date and time....

    }

    Regards,

    --- Bill Angel
     
    Bill Angel, Sep 28, 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. .Net Sports
    Replies:
    1
    Views:
    2,660
    intrader
    Jun 25, 2005
  2. Kyote
    Replies:
    16
    Views:
    4,188
    Michael Borgwardt
    Oct 24, 2003
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,302
    Michael Borgwardt
    May 30, 2004
  4. shilpa
    Replies:
    8
    Views:
    3,916
    Chris Uppal
    Sep 5, 2005
  5. Scott Harper
    Replies:
    2
    Views:
    13,727
    Scott Harper
    Apr 3, 2006
Loading...

Share This Page