2

我很难理解java.sql.timestamp

如果我运行不推荐使用的 java 构造函数:

java.sql.Timestamp(106,2,26,1,0,0,0)
java.sql.Timestamp(106,2,26,2,0,0,0)
java.sql.Timestamp(106,2,26,3,0,0,0)   //<-- Separated by one hour

我得到:

2006-03-26 01:00:00.0
2006-03-26 03:00:00.0     
2006-03-26 03:00:00.0   //<--These last two are the same

夏令时发生在这些时间(至少在我的国家)。但时间前后的日期不会移动。为什么两个不同的时间会同时返回?

我想获取时间戳作为我的输入,我该如何强制?

4

3 回答 3

1

Don't use this deprecated constructor, which is deprecated precisely because it uses the default time zone.

Use a Calendar (or a DateFormat) with the appropriate time zone (CET), set the fields of the calendar (or parse a string containing the date you want to insert), get the milliseconds from the calendar/date, and construct a Timestamp from the milliseconds.

于 2013-02-18T16:58:50.697 回答
0

使用System.currentTimeMillis();为您提供不受夏令时、闰秒和其他日期意外调整影响的格林威治标准时间。

long now = System.currentTimeMillis();

或手动指定时区:

long ms = Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTimeInMillis();

资料来源:在 Java 中获取 GMT 时间

于 2013-02-18T14:30:26.640 回答
0
于 2019-02-07T00:56:18.087 回答