简体   繁体   English

如何将Joda DateTime转换为UTC MySql时间戳

[英]How to convert Joda DateTime to UTC MySql timestamp

I have a Joda DateTime object representing a UTC time, and wish to store it in a Timestamp field in a MySql table. 我有一个表示UTC时间的Joda DateTime对象,并希望将其存储在MySql表的Timestamp字段中。

I have the following code: 我有以下代码:

String ztime = "2013-10-07T08:00:00Z";  

DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
DateTime dt = parser.parseDateTime(ztime).withZone(DateTimeZone.UTC);

PreparedStatement stmt = con.prepareStatement("insert into time_test (time) values (?)");
stmt.setTimestamp(1, Timestamp(dt.getMillis()));
stmt.execute();

However, when I look in the database, the time that gets store is out by the difference of my database's timezone from UTC. 但是,当我查看数据库时,获取存储的时间超出了我的数据库时区与UTC的差异。 eg when my database is running in UTC+1, and run the above code to save "08:00Z", in the database the Timestamp shows as 09:00. 例如,当我的数据库以UTC + 1运行,并运行上述代码以保存“08:00Z”时,数据库中的时间戳显示为09:00。

DateTime's getMillis method says " Gets the milliseconds of the datetime instant from the Java epoch of 1970-01-01T00:00:00Z." DateTime的getMillis方法说“从1970-01-01T00:00:00Z的Java纪元获取日期时刻的毫秒数”。 and MySql's Timestamp says: "MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval.", so I presume it's the MySql conversion that's causing the issue, because the millis it's being initialized with is relative to a fixed UTC time, so it has no need to convert from current time zone to UTC. 和MySql的Timestamp说:“MySQL将TIMESTAMP值从当前时区转换为UTC进行存储,然后从UTC返回到当前时区进行检索。”所以我认为这是导致问题的MySql转换,因为毫秒是它的初始化时相对于固定的UTC时间,因此无需从当前时区转换为UTC。

My code to read the data back out into a DateTime works fine, and I get the value out that I put in, but I also need this to work with some 3rd-party code over which I have no control, which expects the Timestamp to be in the correct UTC time. 我将数据读回到DateTime的代码工作正常,我得到了我输入的值,但我还需要这个来处理一些我无法控制的第三方代码,它需要时间戳到在正确的UTC时间。

How do I get the Timestamp field in the database to match my original UTC date/time ? 如何获取数据库中的Timestamp字段以匹配原始UTC日期/时间?

tl;dr TL;博士

Use java.time classes that supplant Joda-Time . 使用取代Joda-Time的 java.time类。

myPreparedStatement.setObject( 
    … , 
    Instant.parse( "2013-10-07T08:00:00Z" )  
)

Retrieve. 检索。

myResultSet.getObject( 
    … ,
    Instant.class 
)

java.time java.time

The Joda-Time project is now in maintenance-mode, recommending migration to its successor, the java.time classes built into Java 8 and later. Joda-Time项目现在处于维护模式,建议迁移到其后续版本,即Java 8及更高版本中内置的java.time类。 Both are led by the same man, Stephen Colebourne. 两人都是由同一个人斯蒂芬·科勒伯恩领导的。 You'll find many of the same concepts in play, so fairly easy to migrate. 你会发现很多相同的概念,所以很容易迁移。

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction). Instant类表示UTC时间轴上的一个时刻,分辨率为纳秒 (最多九(9)位小数)。

Your input string happens to be in standard ISO 8601 format. 您的输入字符串恰好是标准的ISO 8601格式。 The java.time classes use these standard formats by default when parsing/generating strings. 在解析/生成字符串时, java.time类默认使用这些标准格式。 So no need to specify a formatting pattern. 因此无需指定格式化模式。

String input = "2013-10-07T08:00:00Z" ;     // Standard ISO 8601 format.
Instant instant = Instant.parse( input ) ;  // Parses standard ISO 8601 format by default.

The Instant class replaces both java.util.Date and java.sql.Timestamp . Instant类替换java.util.Datejava.sql.Timestamp As of JDBC 4.2 and later, you can directly exchange java.time objects with the database. 从JDBC 4.2及更高版本开始,您可以直接与数据库交换java.time对象。

myPreparedStatement.setObject( … , instant ) ;

And retrieval. 并检索。

Instant instant = myResultSet.getObject( … , Instant.class ) ;

The TIMESTAMP type in MySQL seems to be akin to the SQL-standard TIMESTAMP WITH TIME ZONE type. MySQL中的TIMESTAMP类型似乎类似于SQL标准TIMESTAMP WITH TIME ZONE类型。 So the code above should work appropriately. 所以上面的代码应该适当地工作。


About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310

You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用符合JDBC 4.2或更高版本的JDBC驱动程序 No need for strings, no need for java.sql.* classes. 不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes? 从哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM