TimeSeries + Millisecond timezone shift

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
dweems
Posts: 12
Joined: Sat Feb 20, 2010 9:59 pm
antibot: No, of course not.

TimeSeries + Millisecond timezone shift

Post by dweems » Mon Nov 29, 2010 4:00 am

I have a database where I'm extracting data to generate an XYPlot where the x-axis is a TimeSeries. When I read the data into my software into a GregorianCalendar value and display the date/time, I get the correct value (relative to the database values). Then I load that value into a Millisecond object, which is then used in a call to TimeSeries#add(RegularTimePeriod period, double value) to load a dataset, but when the data is displayed on the chart, the date/time has been shifted the distance to my time zone (Mountain). The weird thing is that when I displayed my original database date with a print statement ( GregorianCalendar#getTime().toString() ), it displays the date in my local time zone. But the value displayed on the graph is shift over another time zone. For example, my database date displayed with the code above generates:
Mon Dec 15 15:14:06 MST 2008
but my graphics program displays:
15 Dec 2008 08:14 MST
using a formatter like DateFormat dateFormat = new SimpleDateFormat( "dd-MMM-yyyy HH:mm z" );

I tried using the constructor for Millisecond where I specified the timezone as GMT (as below where timestamp is a GregorianCalendar value) but that didn't work.

Code: Select all

millisecondTime = new Millisecond( timestamp.getTime(), TimeZone.getTimeZone( "GMT" ), new Locale( "eng" ) );
This shift is a little bit of annoyance but the application works even though the times are not correct. But it seems to create a real problem when I want to use the crosshair feature to retrieve specific values on the plot. The following code fails:

Code: Select all

double xx = this.plot.getDomainCrosshairValue();
long millis = (long) this.plot.getDomainCrosshairValue();
if ( millis <= 0 ) return;
String dateText = this.dateFormat.format( new Date( millis ) );
this.crosshairDateValue.setText( dateText );
TimeSeries ts = this.pricesDataSet.getSeries( 0 );
TimeSeriesDataItem dataItem = ts.getDataItem( new Millisecond( new Date( millis ) ) );
int item = ts.getIndex( new Millisecond( new Date( millis ) ) );
Note that I display the date first in a JLabel (this.crosshairDateValue) and it agrees with what is displayed on the graph. But when I retrieve a TimeSeriesDataItem or simply get the index of the TimeSeries for the Millisecond value returned (I did both to see what is happening), it fails (dataItem = null & item < 0). If I go into my debugger and get a millisecond value from the database via GregorianCalendar and use that value to retrieve an TimeSeriesDataItem, it works! So how can it change from the time it is loaded into a TimeSeries/DataSet and when it is retrieved with a crosshair?

Lord I hope one of you wonderful people out there has a handle on this problem. I've tried everything I can, even changing the timezone in the Millisecond constructor to "MST" or using the fullname option "America/Denver" and nothing works.

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: TimeSeries + Millisecond timezone shift

Post by skunk » Tue Nov 30, 2010 12:55 am

Are you certain that the timezone in your dataitems is the same as the timezone set on the DateAxis and the same as the timezone on your DateFormatter? Did you try using the default Millisecond constructor that just uses the default timezone?

dweems
Posts: 12
Joined: Sat Feb 20, 2010 9:59 pm
antibot: No, of course not.

Re: TimeSeries + Millisecond timezone shift

Post by dweems » Tue Nov 30, 2010 4:25 am

Yes, the DateAxis is using the same SimpleDateFormatter I provided in the first post(see code below), which simply displays the time zone provided in the TimeSeries. And, yes, I originally used the default constructor for Millisecond. Would not the default constructor for DateAxis simply use the time zone provided in the data? I'll try setting it explicitly and see if that makes a difference.

Code: Select all

DateAxis domainAxis = new DateAxis( "Date Time" );
domainAxis.setDateFormatOverride( this.dateFormat );

dweems
Posts: 12
Joined: Sat Feb 20, 2010 9:59 pm
antibot: No, of course not.

Re: TimeSeries + Millisecond timezone shift

Post by dweems » Tue Nov 30, 2010 4:58 am

Oh, wow! I explicitly set all of the time zones all the way from the database, through the TimeSeries and the DateAxis. I set them all to GMT because the dates in the database are GMT. The data is still being converted to MST when displayed but now the crosshair access works perfectly! Not sure how to force the display to GMT but that's fine. Thanks for the suggestion!

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: TimeSeries + Millisecond timezone shift

Post by skunk » Tue Nov 30, 2010 6:03 pm

Try calling setTimeZone(...) on the DateAxis itself. It is not the same as the timezone on the (overridden) formatter

dweems
Posts: 12
Joined: Sat Feb 20, 2010 9:59 pm
antibot: No, of course not.

Re: TimeSeries + Millisecond timezone shift

Post by dweems » Tue Nov 30, 2010 6:21 pm

Yes, that worked wonderfully. Indeed, I'm able to use that to switch back and forth from GMT to the system default, which is the local timezone. Awesome! Many, many thanks!

Locked