postgresql java SimpleDateFormat - Postgres“带时区的时间戳”日期格式的正确格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32202155/
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
java SimpleDateFormat - Correct format for Postgres "timestamp with timezone" date format
提问by Joey Baruch
I'm querying a Postgres database and looking for the best way to store a Postgres "timestamp with timezone" information.
我正在查询 Postgres 数据库并寻找存储 Postgres“带时区的时间戳”信息的最佳方式。
the format is "yyyy-MM-dd' 'HH:mm:ss.SSSzz" (i.e. "2014-04-22 05:39:49.916+03")
格式为“yyyy-MM-dd''HH:mm:ss.SSSzz”(即“2014-04-22 05:39:49.916+03”)
i'd like to use a timestamp oriented type/class to keep the info (not String)
我想使用面向时间戳的类型/类来保留信息(不是字符串)
the following throws Unparsable date for all the TIME_FORMATS
i could think of:
以下为TIME_FORMATS
我能想到的所有抛出无法解析的日期:
final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss.SSSz";
final SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
final java.util.Date utilDate = sdf.parse("2014-04-22 05:39:49.916+03");
回答by Codebender
Your format is having ISO Timezone
, so use X
(not z).
您的格式具有ISO Timezone
,因此请使用X
(而不是 z)。
final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss.SSSX";
回答by Basil Bourque
java.sql.Timestamp
java.sql.时间戳
Current JDBC drivers will create. java.sql.Timestamp
object for you when retrieving a value from either of Postgres' time stamp types, TIMESTAMP WITH TIME ZONE
and TIMESTAMP WITHOUT TIME ZONE
.
将创建当前的 JDBC 驱动程序。java.sql.Timestamp
从 Postgres 的任一时间戳类型TIMESTAMP WITH TIME ZONE
和TIMESTAMP WITHOUT TIME ZONE
.
java.sql.Timestamp ts = myResultSet.getTimestamp( 1 ) ;
No need for strings. Stick to using objects rather than strings for date-time values whenever possible.
不需要字符串。尽可能坚持使用对象而不是字符串作为日期时间值。
java.time
时间
In Java 8 and later you should be using the new java.time package. Avoid the old java.util.Date/.Calendar and so on.
在 Java 8 及更高版本中,您应该使用新的 java.time 包。避免使用旧的 java.util.Date/.Calendar 等。
When your JDBC driver is updated for JDBC 4.2, you will be able to retrieve your Postgres date-time value directly into the java.time types.
当您的 JDBC 驱动程序针对JDBC 4.2更新时,您将能够将 Postgres 日期时间值直接检索到 java.time 类型中。
While waiting on such drivers, use the new conversion methods added to java.sql.Timestamp for going toand froman Instant
.
虽然这样司机在等着,用加入的java.sql.Timestamp换去新的转换方法,以和从一个Instant
。
Search StackOverflow for more info as this has been discussed many times already.
搜索 StackOverflow 以获取更多信息,因为这已经讨论过很多次了。
回答by mthmulders
If you're using Java 8, you could store the data in a ZonedDateTime
object, which stores a
如果您使用的是 Java 8,则可以将数据存储在一个ZonedDateTime
对象中,该对象存储一个
date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.
日期时间与 ISO-8601 日历系统中的时区,例如 2007-12-03T10:15:30+01:00 Europe/Paris。
Simple call toInstant()
on the java.sql.Timestamp
value in the result and you're good to go (thanks @Basil Bourque for pointing this out).
简单地调用结果中toInstant()
的java.sql.Timestamp
值,你就可以开始了(感谢@Basil Bourque 指出这一点)。
Prior to Java 8, you could use your existing solution. Note however that if your timezone info is stored as +03
, you should have your time format pattern end with X
, which is a ISO 8601 time zone.
在 Java 8 之前,您可以使用现有的解决方案。但是请注意,如果您的时区信息存储为+03
,则您的时间格式模式应该以 结尾X
,这是 ISO 8601 时区。