TIME ZONES & DAYLIGHT SAVINGS TIME
I am trying to pull time series data in UTC from a database. When I do a simple select query, and print the data, it seems the Calendar object is doing some DST conversion, and I get duplicate timestamps. See below:
KPDX, '2003-03-29 23:00:00', 69
KPDX, '2003-03-30 00:00:00', 70
KPDX, '2003-03-30 01:00:00', 69
KPDX, '2003-03-30 01:00:00', 68
So, since the machine is running in a different timezone than UTC, I set the Calendar object timezone to UTC. I still get the same results as above with the dupliate times. I want to get the following result:
KPDX 2003-03-29 23:00:00 69
KPDX 2003-03-30 00:00:00 70
KPDX 2003-03-30 01:00:00 69
KPDX 2003-03-30 02:00:00 68
Just as it is stored in our database. When I set the timezone environment varable to TZ=UTC, it works fine. Since this will be running on a MICROSOFT platform, I need this functionality without having to modify a timezone environment variable.
Here is the code I'm using to pull the data:
StringBufferbuff =new StringBuffer();
DbConn conn =new DbConn( args );
ResultSet rslt;
String lid, value;
MikeTimet=new MikeTime();
SimpleTimeZonestz =new SimpleTimeZone( 0,"UTC" );
t.setTimeZone( stz );
buff.append("SELECT * FROM metar2:temp_obs WHERE lid='KPDX' order by ot asc;" );
rslt = conn.exSql( buff.toString() );
buff.delete( 0, buff.capacity() );
while( rslt.next() )
{
lid = rslt.getString(1);
t.setTime( rslt.getTimestamp(2) );
value = rslt.getString(3);
System.out.println( lid +", '" + t.prSqlTime() +"', " + value );
}
rslt.close();
conn.close();
What am I doing wrong?
[2109 byte] By [
helper5a] at [2007-10-2 15:47:35]

ps. if you can get the time as a normal java.util.Date object, you can format it in UTC using a DateFormat or SimpleDateFormat.import java.text.SimpleDateFormat;
import java.util.TimeZone;
...
SimpleDateFormat format = new SimpleDateFormat(pattern);
format.setTimeZone(TimeZone.getTimeZone("UTC");
Date now = new Date();
String formattedNow = format.format(now);
see also http://javaalmanac.com/egs/java.text/FormatDate.html
http://java.sun.com/docs/books/tutorial/i18n/format/dateintro.html
> I have tried this with the Gregorian Calander class
> (from which MikeTime derives) alone, and I get the
> same results.
>
What about the code snippet that I posted (once one adds the missing ')')
> I'm not sure I understand your question. The
> MikeTime.prSqlTime() returns the'2003-03-02
> 01:00:00' portion of the output.
Exactly, so that's the method you need to fix. Unfortunately you didn't post what it does, so suggesting how to fix it is pretty hard.
I FOUND A FIX FOR THE PROBLEM ABOVE.
First of all, the original code above is just fine. My problem was a lack of understanding of the Java Runtime Environment. I discovered that regardless of all the time zone setting you do in the code, the JRE still thinks it is in the time zone that the computer OS is set to. So by declaring a Calendar object with a UTC time zone, the JRE was still in PST8PDT (or it thinks it is) because the OS is set to that time zone. Despite my Calendar object being defined with a UTC time zone, I still got daylight savings time behavior in my time series data.
The fix was painfully simple (as about 95% of them are). I just added the following line of code as the first line of code after the main() declration.
System.setProperty( "user.timezone", "UTC" );
Any Calendar and/or Timezone objects I declared to be in UTC after that behaved properly.My understanding is: the JRE now thinks it is in UTC, and doesn't do any of the daylight savings time stuff to the time series data.
Again,
HUGE THANKS to all that responded.
Especially 'jsalonen' for your prompt and patient help!