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]
# 1
The problem is in one of the classes of your own:> MikeTimet= new MikeTime();> ...> System.out.println( lid + ", '" + t.prSqlTime() + "', " + value );What string does MikeTime.prSqlTime() return?
jsalonena at 2007-7-13 15:47:52 > top of Java-index,Java Essentials,Java Programming...
# 2

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

jsalonena at 2007-7-13 15:47:52 > top of Java-index,Java Essentials,Java Programming...
# 3
I have tried this with the Gregorian Calander class (from which MikeTime derives) alone, and I get the same results.I'm not sure I understand your question. The MikeTime.prSqlTime() returns the'2003-03-02 01:00:00' portion of the output.
helper5a at 2007-7-13 15:47:52 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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.

jsalonena at 2007-7-13 15:47:52 > top of Java-index,Java Essentials,Java Programming...
# 5

I tried your code using a SimpleDateFormat. I got the same duplicate results. I get the same duplicate results just using a GregorianCalendar as well. I'm developing on a LINUX box, so just setting the TZ environment variable will solve the problem. However, this code will eventually need to run on a MICROSOFT box. Unless you know of a way of easily changing the timezone to UTC via a batch file, I need to do this in the Java Code.

Thanks so much for your help to this point! I very much appreciate your fast response!

helper5a at 2007-7-13 15:47:52 > top of Java-index,Java Essentials,Java Programming...
# 6

I ran jsalonen's code. It's about 3:40 in the afternoon here and I'm in GMT-0800. It printed "Mar 23, 2006 11:40:40 PM". So it works. If you don't think it does, then your inputs probably aren't what you think they are either. More detailed investigation would be required to find out what your actual problem is.

(You made a subclass of GregorianCalendar? What for?)

DrClapa at 2007-7-13 15:47:52 > top of Java-index,Java Essentials,Java Programming...
# 7
> I tried your code using a SimpleDateFormat.> I got the same duplicate results. Then you must have duplicate entries in the databasethere is no DST in UTC, so different dates are formatted differently.Can you show how exactly prSqlTime calculates its result?
jsalonena at 2007-7-13 15:47:52 > top of Java-index,Java Essentials,Java Programming...
# 8

> so different dates are formatted differently.

>

I meant, distinct dates will be formatted into distinct strings.

Actually, that should happen with all time zones, I wasn't thinking clearly.

Are you sure the date that the database returns doesn't depend on the time zone setting?

jsalonena at 2007-7-13 15:47:52 > top of Java-index,Java Essentials,Java Programming...
# 9

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!

helper5a at 2007-7-13 15:47:52 > top of Java-index,Java Essentials,Java Programming...