java date issues

L

lambelly

I'm having a rather annoying date inconsistency error.

This code:
System.setProperty("user.timezone", "GMT");
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy/MM/dd
HH:mm:ss");
java.util.Date day = new java.util.Date();
Calendar calendar = new
GregorianCalendar(TimeZone.getTimeZone("GMT"));
calendar.setTime(day);

coupled with this query:
Sql = "UPDATE RPA SET " +
"VERIFICATION_ID = " + n_VeriStatus +
", NAME = 'Refer to Contract', " +
"ACCOUNT = '00000000000', " +
"SITE = '000', " +
"ACCEPTANCE_TYPE = 'IVR', " +
"ACTION_TIME = to_date('" +
formatter.format(calendar.getTime()) + "', 'yyyy/mm/dd hh24:mi:ss') " +

is sometimes placing an inconsistent value in the database. What I mean
by that is, usually it seems to convert to GMT time normally. Other
times the time is off on the date are by a few hours. There doesn't
appear to be any pattern to when it is off. My initial assumptions are
that this has to do with this code maybe running on different machines
perhaps on different versions of the JDK. Can anyone else think of
reasons this code might not convert to GMT properly or fixes to this
code?
 
P

P.Hill

System.setProperty("user.timezone", "GMT");

Okay, so you want to set the default Timezone. May I suggest you
use the API provided for that:

TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy/MM/dd
HH:mm:ss");

This is good, you have a created a formatter for exactly what you want.
java.util.Date day = new java.util.Date();

Okay, now you've gotten the current time...
Calendar calendar = new
GregorianCalendar(TimeZone.getTimeZone("GMT"));
calendar.setTime(day);

Then you push it into calendar, just so you can pull it out.
THE ABOVE THREE LINES DO NOTHING, regardless of what A java.lang.Date
is a binary timestamp, despite what the toString() shows you, thus I'd
guess you are confused as to what you need to do with this value.

Also, why bother to set the default, if you are explicitly setting a
timezone into a calendar?

The calendar and timezone you need to worry about are the ones
in SimpleDateFormat. Each DateFormat uses a Calendar to work out how to
convert a binary timestamp to separate fields. It also uses the
Timezone contained in the Calendar indirectly.

You can set a SimpleDateFormat with an explicit Calendar which
can be set with an explicit TZ.

I would suggest adding some logging in the program that writes out
a longer version of the datetime which includes the timezone or its
offset. Can you get at logs from different machines running your app?
I would suggest adding ZZZZ to the log format, so you can track the
timezone used.
coupled with this query:

When you say "coupled", do you mean somewhere later in your program?
Is it possible you are not always executing the entire sequence?

Right off the top of my head, I think your code should work, but I'm
not convinced the setProperty technique is a documented way to change
the default. Maybe someone is running an old JVM or even a different
JVM which doesn't support the user.timezone setting method.

Sql = "UPDATE RPA SET " +
"VERIFICATION_ID = " + n_VeriStatus +
", NAME = 'Refer to Contract', " +
"ACCOUNT = '00000000000', " +
"SITE = '000', " +
"ACCEPTANCE_TYPE = 'IVR', " +
"ACTION_TIME = to_date('" +
formatter.format(calendar.getTime()) + "', 'yyyy/mm/dd hh24:mi:ss') " +

My initial assumptions are
that this has to do with this code maybe running on different machines
perhaps on different versions of the JDK. Can anyone else think of
reasons this code might not convert to GMT properly or fixes to this
code?

The different JVM hypothesis sounds like a good guess.

If you can understand why the following code produces the same result
formated in GMT for both the day value and the Date value in the
Calendar even though (1) I didn't change the default timezone and (2) I
pushed the date into and out of calendar which was explicitly set to the
local timezone (which was NOT GMT), then you'll be on your way to
understand where TZs are relevant to converting times.

TimeZone local = TimeZone.getDefault();
TimeZone gmt = TimeZone.getTimeZone("GMT");

SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy/MM/dd HH:mm:ss zzz");
formatter.setTimeZone(gmt);

java.util.Date day = new java.util.Date();
System.out.println( formatter.format( day ));

Calendar calendar = new GregorianCalendar();
calendar.setTimeZone(local); // <-- this calendar is set to
local time
calendar.setTime(day);
System.out.println( formatter.format( calendar.getTime() ));

My results were:
2006/06/06 01:27:34 GMT
2006/06/06 01:27:34 GMT

-Paul


p.s. Trim the email to get an email back to me, if you have more
questions or reply in group, you aren't the first nor the last which
misinterpretted how these objects work.
 
L

lambelly

I wanted to thank you for the thorough response. I am a really new java
developer who sometimes feels like he's in a bit over his head. I
really appreciate how completely you explained these objects. I was
able to apply what you had said to find what I think is a fairly decent
solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top