#35 DateParser error (getDate on headers)

v1.0 (example)
open
nobody
Bug (26)
5
2011-01-20
2011-01-20
No

When parsing dates using DateParser (org.simpleframework.http.parse) one can get inconsistent results from toLong given the same value. For example, the code:

String date = "Thu, 20 Jan 2011 16:43:08 GMT";

DateParser dp = new DateParser(date);
System.out.println("value a: " + dp.toLong());
Thread.sleep(50);

dp = new DateParser(date);
System.out.println("value b: " + dp.toLong());
Thread.sleep(50);

dp = new DateParser(date);
System.out.println("value c: " + dp.toLong());

produced for me:

value a: 1295541788802
value b: 1295541788853
value c: 1295541788903

This gets back to not clearing the millisecond field in the Calendar instance. Take the code from toLong and run it directly:

TimeZone zone = TimeZone.getTimeZone("GMT");

Calendar calendar = Calendar.getInstance(zone); /* GMT*/
calendar.set(2011, 0, 20, 16, 43, 8);
System.out.println("value 1: " + calendar.getTime().getTime());
Thread.sleep(50);

calendar = Calendar.getInstance(zone); /* GMT*/
calendar.set(2011, 0, 20, 16, 43, 8);
System.out.println("value 2: " + calendar.getTime().getTime());

I got:

value 1: 1295541788697
value 2: 1295541788749

The solution is to clear the millseconds:

Calendar calendar = Calendar.getInstance(zone); /* GMT*/
calendar.set(2011, 0, 20, 16, 43, 8);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime().getTime();

Discussion

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks