2

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

2
  • 1
    Are you sure they are on the same timezone? What do you get if you print new Date(d.getTime());? Commented Jan 21, 2014 at 14:29
  • 1
    java.sql.Time is wrapper class to java.util.Date. Could not you use directly this class? If yes, than you could also use Calendar. And than your problem should be solved. Commented Jan 21, 2014 at 14:35

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

3 Comments

But what should I do now? How can I use this with Time?
You will have to express your time in the same timezone but I have the feeling you are misusing the getTime() method. Could you please explain what your are trying to do? Why do you want to have the same value on all machines?
@Aligz I just need this to be worked same on all machines. Its a part of a JPA project(with postgresql db with a time field/column). Besides I am using four machines on my cluster.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.