TimeZone discrepancy in mysql and java

前端 未结 5 424
生来不讨喜
生来不讨喜 2021-01-18 05:36

I have a query in mysql which compares 2 dates like this

convert_tz(updatedDate,\'+05:30\',\'-05:00\') < ?

the convert function returns

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-18 06:26

    Background: A surprisingly common--and big--misconception shared by even brilliant programmers is the notion that stored time stamps (in your database, Date, Calendar, Timestamp, et al) somehow have time zone information. They do not. A time stamp (up until Java 8, anyway) is stored as the number of milliseconds since midnight on 1 Jan 1970 UTC. End of sentence. The only thing setting the time zone does is provide enough information to the computer to convert that time stamp to a human readable format, and vice versa.

    Answer: When you suspected that this was a time zone problem, you were right. But the code you used to try to verify this also has a problem:

    end.setTimeZone(TimeZone.getTimeZone("America/New York"));
    pst.setTimestamp(1, new java.sql.Timestamp(end.getTimeInMillis()));
    

    That setTimeZone statement has no effect on the time stored in end, because the time has already been set. It would only have had an effect if you stored the time afterwards, and then only if you used one of Calendar's methods which converted the time from a human readable format (and not setTimeInMillis).

    When you use getTimeInMillis to pass the time stamp to your prepared statement, you're retrieving the time stamp directly. Since you're not converting it to a human format, once again the time zone information is ignored.

    When you try

    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    end.setTime(sdf.parse("2012-10-01 00:00:00"));
    pst.setTimestamp(1, new java.sql.Timestamp(end.getTime()));
    

    and

    pst.setTimestamp(1, new java.sql.Timestamp(octDate.get(Calendar.YEAR)-1900,octDate.get(Calendar.MONTH),octDate.get(Calendar.DATE),octDate.get(Calendar.HOUR),octDate.get(Calendar.MINUTE),octDate.get(Calendar.SECOND),0));
    pst.setTimestamp(2, new java.sql.Timestamp(end.get(Calendar.YEAR)-1900,end.get(Calendar.MONTH),end.get(Calendar.DATE),end.get(Calendar.HOUR),end.get(Calendar.MINUTE),end.get(Calendar.SECOND),0));
    

    things appear to work because you are now using methods which convert to/from a human readable format, and therefore the specified time zone information is used. However, this is only covering up the real problem. The real issue is that the time was improperly converted when you parsed it from endString. That is, the time zone that endString was expressed in does not match the time zone set in df1 at the time the date was parsed.

    SHORT ANSWER: before this line:

    end.setTime(df1.parse(endString));
    

    You need to:

    • Figure out what time zone the time in endString was expressed in.
    • Set df1 and not end to that same time zone. Since df1 is the thing that is converting the date from human format, it's that time zone information that's used.

    Cheers!

提交回复
热议问题