Problems Parsing Date string with SimpleDateFormat

B

Bill Angel

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
 
M

Marko Lahma

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
 
B

Bill Angel

Marko Lahma said:
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
 
M

Marko Lahma

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
 
B

Bill Angel

Marko Lahma said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top