且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java.sql.Timestamp:更改Timestamp的时区

更新时间:2023-02-07 14:54:02

java.sql.Timestamp 对象没有时区 - 它们是时间瞬间,如 java。 util.Date

java.sql.Timestamp objects don't have time zones - they are instants in time, like java.util.Date.

如果您认为它们处于特定时区,您可能会因为误导而感到困惑输出(例如,使用默认时区自动将该瞬间转换为本地时间)或者您可能以不适当的方式创建数据。您需要的答案将取决于您的具体情况。

If you're thinking of them as being in a particular time zone, you may either be getting confused due to misleading output (e.g. something automatically converting that instant into a local time using a default time zone) or you may have created the data in an inappropriate way. The answer you need will depend on the details of your situation.

例如,如果您只想显示 a 时间戳特定时区的值,您可以使用 SimpleDateFormat ,适当设置时区,然后格式化时间戳(因为它扩展日期)。我不相信这会让你在内部显示与时间戳存储一样多的精确度,但可能对你来说不是问题。

For example, if you just want to display a Timestamp value in a particular time zone, you can use SimpleDateFormat, set the time zone appropriately, and just format the Timestamp (as it extends Date). I don't believe that will allow you to display as much precision as the timestamp stores internally, but that may not be a problem for you.

如果您的数据创建不正确,那么可能有也可能没有纠正它的方法 - 例如,由于夏令时变化,可能会有一些歧义。但是,我们越了解它就越能帮助您。

If your data has been created incorrectly then there may or may not be a way to "correct" it - there may be some ambiguities due to daylight saving time changes, for example. However, the more we know about it the better we'll be able to help you.