Parsing date and time in the common log format.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Yash_Sathaye
Posted On:   Saturday, January 26, 2002 01:42 PM

I am trying to parse time in the format below: 25/Mar/2001:00:00:35 +0600 and get the time in milliseconds since the Epoch. I am using the time format pattern string "dd/mmm/yyyy:HH:mm:ss +HHmm" to construct a SimpleDateFormat and parsing it. Then I hope to use getTime() on the Date object get from the parse method to get the milliseconds. I do not get any errors while parsing the date but when I output the Date I get back in the current locale (EST time zone) I get the time as Mar 25,2001 06:00:35 EST Which is obviously not correct. (GMT +0600 correspods to time in China and that time zone is 11 hours ahead of EST). What am I missi   More>>


I am trying to parse time in the format below:

25/Mar/2001:00:00:35 +0600

and get the time in milliseconds since the Epoch.

I am using the time format pattern string


"dd/mmm/yyyy:HH:mm:ss +HHmm"


to construct a SimpleDateFormat and parsing it. Then I hope to use getTime() on the Date object get from the parse method to get the milliseconds.

I do not get any errors while parsing the date but when I output the Date I get back in the current locale (EST time zone) I get the time as


Mar 25,2001 06:00:35 EST


Which is obviously not correct. (GMT +0600 correspods to time in China and that time zone is 11 hours ahead of EST). What am I missing?

I have doubts about the time format pattern string I am using. The SimpleDateFormat's docs are very unclear about parsing this time format.

Please let me know. Any help greatly appreciated.

Thanks    <<Less

Re: Parsing date and time in the common log format.

Posted By:   Stephen_Ostermiller  
Posted On:   Wednesday, February 13, 2002 09:14 AM

From what I can tell, the SimpleDateFormat cannot handle the time zone information in that format. Here is some code that I wrote at one point:



private static SimpleDateFormat logDateFormat = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
static {
logDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
logDateFormat.setLenient(false);
}
private static Calendar calendar = Calendar.getInstance();

/**
* Parse the date.
* Should be in the format dd/MMM/yyyy:HH:mm:ss +0000
*/
public static Date parseLogDate(String s) throws ParseException {
int length = s.length();
Date d = logDateFormat.parse(s, new ParsePosition(0));
if (d == null){
throw new ParseException ("Could not parse the date: " + d, 0);
}
calendar.setTime(d);
if (length < 5){
throw new ParseException ("Could not parse the Time Zone on the date: " + d, 0);
}
try {
int hours = Integer.parseInt(s.substring(length-5,length-2));
int minutes = Integer.parseInt(s.substring(length-1));
hours = 0 - hours;
if (hours < 0) {
minutes = 0 - minutes;
}
calendar.add(Calendar.HOUR, hours);
calendar.add(Calendar.MINUTE, minutes);
return calendar.getTime();
} catch (NumberFormatException x){
throw new ParseException ("Could not parse the Time Zone on the date: " + d, 0);
}
}
About | Sitemap | Contact