4 messages in com.mysql.lists.javaRe: help with preparedStatement.setTi...
FromSent OnAttachments
Dan Jatnieks13 Apr 2006 09:29 
Shankar Unni17 Apr 2006 13:00 
Dan Jatnieks19 May 2006 17:13 
Shankar Unni22 May 2006 12:59 
Subject:Re: help with preparedStatement.setTimestamp with Calendar
From:Shankar Unni (shan@netscape.net)
Date:04/17/2006 01:00:13 PM
List:com.mysql.lists.java

Dan Jatnieks wrote:

e.g. I have 2006-01-01 00:00:00 (UTC) as the timestamp value and expect that when it is written to the database it will become 2005-12-31 19:00:00 (EST).

So far, so good.

Calendar utc = Calendar.getInstance(TimeZone.getTimeZone("GMT")); Calendar cal = Calendar.getInstance("EST"); PreparedStatement stmt = m_conn.prepareStatement(sql);

...

// Use the UTC Calendar to create a new timestamp and the target // Calendar to adjust it to the correct timezone and set the // timestamp value. Timestamp tstamp = new Timestamp(utc.getTimeInMillis()); stmt.setTimestamp(2, tstamp, cal);

But this isn't doing anything (at least, I don't see how you plan on getting the time "00:00:00" from this. How do you set the time in the calendar called "utc"? By default, a newly-created calendar contains the current date and time.

Did you do

utc.set(Calendar.HOUR_OF_DAY, 0); ...

etc., to set the time in "utc", before you called utc.getTimeInMillis()?

By the way, a word of warning: if your program is multi-threaded, don't try to share a Calendar instance globally like this (for "cal", or for "utc", for that matter). Calendar is not thread-safe. Always allocate a local Calendar instance on stack and use/modify it on the spot.