Does Java's `java.sql.Timestamp` Storage Depend on the Time Zone?
Is java.sql.Timestamp's Storage Influenced by Time Zone?
In JDBC, the setTimestamp() method's behavior with java.sql.Timestamp may vary based on the driver implementation.
Driver's Behavior:
Most drivers adhere to the rules outlined in the javadoc for setTimestamp(int parameterIndex, Timestamp x, Calendar cal). If no Calendar object is provided, the driver typically uses the time zone of the virtual machine running the application.
Timestamp Storage in the Database:
When using setTimestamp(int parameterIndex, Timestamp x) without specifying a Calendar, the driver converts the timestamp to the VM's time zone before storing it in the database. If the database column lacks time zone information, this conversion may result in the loss of time zone data.
Example:
Consider a local time zone of GMT 2 and a UTC timestamp of "2012-12-25 10:00:00 UTC." After storage in the database, the timestamp appears as "2012-12-25 12:00:00" (GMT 2). If retrieved by an application in GMT 0, the timestamp would be interpreted as "2012-12-25 12:00:00 UTC."
Storing Timestamps in Specific Time Zones:
To store timestamps in a specific time zone, use setTimestamp(int parameterIndex, Timestamp x, Calendar cal) with the desired Calendar instance (e.g., Calendar.getInstance(TimeZone.getTimeZone("GMT")) for GMT).
Timestamp Retrieval:
Similarly, when retrieving timestamps, ensure that you use the same time zone as when storing them.
Using java.time.LocalDateTime with JDBC 4.2 :
In JDBC 4.2 compliant drivers, you can use java.time.LocalDateTime (and java.time.LocalTime) with TIMESTAMP and TIME types through the get/set/updateObject methods. These classes represent time without time zones.
The above is the detailed content of Does Java's `java.sql.Timestamp` Storage Depend on the Time Zone?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

