JCommon 5.6 Hour Bug

A discussion forum for the JCommon class library.
Locked
Elijah C. Menifee

JCommon 5.6 Hour Bug

Post by Elijah C. Menifee » Tue Mar 19, 2002 10:05 pm

Using the following code:
public static void main(String[] args) {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MM-dd-yyyy HH:mm:ss.SSS");
Date date = new Date(2002-1900,2,15);
Day day1 = new Day(date);
Hour h1 = new Hour(0,day1);
long day1start = day1.getStart();
long diff = date.getTime() - day1start;
long hourstart = h1.getStart();
long hourend = h1.getEnd();
System.out.println("data hour1 start: " + sdf.format(new Date(hourstart)));
System.out.println("data hour1 end: " + sdf.format(new Date(hourend)));
System.out.println("hourstart: " + hourstart);
System.out.println("hourend: " + hourend);
}

i get the following:
data hour1 start: 03-14-2002 23:00:00.000
data hour1 end: 03-14-2002 23:59:59.999
hourstart: 1016168400000
hourend: 10161711016168400000I999999
after fixing day.getStart() , day.getStart() returns the start of the day which should be
the same as the start of hour 0 in that day

changing the following:
public long getStart(TimeZone zone) {
return this.day.getStart(zone)+(this.hour-1)*MILLISECONDS_PER_HOUR;
}

public long getStart(int offset) {
return this.day.getStart(offset)+(this.hour-1)*MILLISECONDS_PER_HOUR;
}
to:
public long getStart(TimeZone zone) {
return this.day.getStart(zone)+(this.hour)*MILLISECONDS_PER_HOUR;
}

public long getStart(int offset) {
return this.day.getStart(offset)+(this.hour)*MILLISECONDS_PER_HOUR;
}

I get:

data hour1 start: 03-15-2002 00:00:00.000
data hour1 end: 03-15-2002 00:59:59.999
hourstart: 1016172000000
hourend: 1016175599999

Which is what I expect.

The following patch also includes a toString method, and the main method, which should probably be changed to a junit test for future integrety testing:

--- jcommon-0.5.6/source/com/jrefinery/data/Hour.java Wed Mar 6 10:55:07 2002
+++ /home/emenifee/jcommon-0.5.6/source/com/jrefinery/data/Hour.java Tue Mar 19 14:43:58 2002@@ -212,11 +212,11 @@
* Returns the first millisecond of the hour.
*/
public long getStart(TimeZone zone) {
- return this.day.getStart(zone)+(this.hour-1)*MILLISECONDS_PER_HOUR;
+ return this.day.getStart(zone)+(this.hour)*MILLISECONDS_PER_HOUR;
}

public long getStart(int offset) {
- return this.day.getStart(offset)+(this.hour-1)*MILLISECONDS_PER_HOUR;
+ return this.day.getStart(offset)+(this.hour)*MILLISECONDS_PER_HOUR;
}

/**
@@ -255,5 +255,27 @@
return result;

}
+ public String toString() {
+
+ java.text.DecimalFormat df = new java.text.DecimalFormat("00");
+ return day.toString() + " " + df.format(hour);
+
+ }
+ public static void main(String[] args) {
+ java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MM-dd-yyyy HH:mm:ss.SSS");
+ Date date = new Date(2002-1900,2,15);
+ Day day1 = new Day(date);
+ Hour h1 = new Hour(0,day1);
+ long day1start = day1.getStart();
+ long diff = date.getTime() - day1start;
+ long hourstart = h1.getStart();
+ long hourend = h1.getEnd();
+ System.out.println("data hour1 start: " + sdf.format(new Date(hourstart)));
+ System.out.println("data hour1 end: " + sdf.format(new Date(hourend)));
+ System.out.println("hourstart: " + hourstart);
+ System.out.println("hourend: " + hourend);
+
+ }
+

}

Locked