将 java.util.Calendar ISO 8601 格式转换为 java.sql.Timestamp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32461307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert java.util.Calendar ISO 8601 format to java.sql.Timestamp
提问by José Mendes
I have a date in ISO 8601 date format 2015-09-08T01:55:28Z
. I used this code to convert the ISO 8601 fate to a Calendar object:
我有一个 ISO 8601 日期格式的日期2015-09-08T01:55:28Z
。我使用此代码将 ISO 8601 命运转换为 Calendar 对象:
Calendar cal = javax.xml.bind.DatatypeConverter.parseDateTime("2015-09-08T01:55:28Z");
and now I need to use the cal.getTime()
to get my time, but I need to convert it to a java.sql.Timestamp
. I tried to do this:
现在我需要使用cal.getTime()
来获取我的时间,但我需要将其转换为java.sql.Timestamp
. 我试图这样做:
final Timestamp finalDate = (Timestamp) cal.getTime();
but I got this error:
但我收到了这个错误:
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Timestamp
Ideas?
想法?
回答by Puce
As the exception says: Calendar::getTime()
returns a java.util.Date
object, not a java.sql.Timestamp
object. So you cannot cast it to a Timestamp object.
正如异常所说:Calendar::getTime()
返回一个java.util.Date
对象,而不是一个java.sql.Timestamp
对象。所以你不能将它转换为 Timestamp 对象。
Use:
利用:
Timestamp timestamp = new Timestamp(cal.getTimeInMillis());
And also consider to replace Calendar
with the new Date & Time APIintroduced in Java SE 8.
并且还考虑替换Calendar
为Java SE 8 中引入的新Date & Time API。
回答by Basil Bourque
The Answerby Puce is correct.
Puce的答案是正确的。
java.time
时间
The modern way in Java 8 and later is to use the new java.timeframework. These new classes supplant the old java.util.Date/.Calendar that have proven to be confusing and troublesome.
Java 8 及更高版本中的现代方法是使用新的java.time框架。这些新类取代了旧的 java.util.Date/.Calendar,后者已被证明是令人困惑和麻烦的。
An Instant
is a moment on the timeline, a count of nanoseconds from first moment of 1970 in UTC. The class is able to parse strings such as yours that comply with the ISO 8601 format.
AnInstant
是时间线上的一个时刻,从 UTC 时间 1970 年的第一个时刻开始计算的纳秒数。该类能够解析符合 ISO 8601 格式的字符串。
String input = "2015-09-08T01:55:28Z";
Instant instant = Instant.parse( input );
Database via old java.sql
types
通过旧java.sql
类型的数据库
From an Instant, we can get a java.sql.Timestampby calling the from
method newly added to this old class.
从 Instant 中,我们可以通过调用新添加到这个旧类的方法来获取java.sql.Timestampfrom
。
java.sql.Timestamp ts = java.sql.Timestamp.from( instant );
We could combine all three lines into a one-liner, not that I recommend it (makes debugging more difficult).
我们可以将所有三行合并为一行,这不是我推荐的(使调试更加困难)。
java.sql.Timestamp ts = Timestamp.from( Instant.parse( "2015-09-08T01:55:28Z" ) );
Database via java.time
types
数据库通过java.time
类型
As of JDBC 4.2, a compliant JDBC drivershould be able to pass the java.time types via getObject
and setObject
on a PreparedStatement
. If your driver does not, use the conversion methods for the old classes.
由于JDBC 4.2,兼容JDBC驱动程序应该能够通过传递java.time类型getObject
和setObject
上一个PreparedStatement
。如果您的驱动程序没有,请使用旧类的转换方法。
myPreparedStatement.setObject( 1 , instant );