Sabuj Hassan
Sabuj Hassan

Reputation: 39365

Different output generated by java.sql.Time from different server

I have tested the following code in four machines. Two of those are windows(win7 & win8), and two are linux(centos). Output on the windows pc are same(but different with linux).

java.sql.Time d = java.sql.Time.valueOf("19:54:17");
System.out.println(d.getTime());

Windows output: 50057000
Linux output: 93257000

Windows: java version "1.6.0_26"
Linux: java version "1.6.0_24"

Times are slightly different amongst the machines(in 2-3 minutes). But Windows are having UTC+6 and Linux are having GMT+6 timezone.

Another info. I have also had a run this code on this online Java compiler and it shows the output 93257000

I just want to have equal output from all my machines. What should I do?

Update: as you requested the output for this code System.out.println(new Date(d.getTime())):

Windows: Thu Jan 01 19:54:17 ALMT 1970
Linux: Thu Jan 01 19:54:17 GMT-06:00 1970
Online Compiler: Thu Jan 01 19:54:17 CST 1970

Upvotes: 2

Views: 179

Answers (2)

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

I found the solution by using the following one with mine one. This makes my long value same throughout the machines.

TimeZone.setDefault(TimeZone.getTimeZone("PST"));

More detail about this can be found at https://bugs.java.com/bugdatabase/view_bug?bug_id=4487450

Upvotes: 0

NeplatnyUdaj
NeplatnyUdaj

Reputation: 6242

Looks like bad conversion of timezone

50057000 = 13:54:17 = 19:56:17 - 6
93257000 = 25:54:17 = 19:56:17 + 6

Not really a solution, but the formatting wouldn't look so nice in comments.

I think you are losing the timezone information in the valueOf method. d.getTime().toString() will convert the time based on locale. Check if you have same timezone in java:

Calendar.getInstance().getTimeZone();
java.sql.Time.valueOf("19:54:17").getTimezoneOffset();

Upvotes: 1

Related Questions