[英]How to convert Snowflake timestamp to Date
can anyone help me suggest how can i convert snowflake ltz timestamp to given date using java?谁能帮我建议如何使用 java 将雪花 ltz 时间戳转换为给定日期?
Sample in snowflake ltz format: 1658443006.285000000雪花 ltz 格式的样本:1658443006.285000000
While I do not use Snowflake, a quick look at the documentation shows that their TIMESTAMP_LTZ
type is akin to the SQL standard type TIMESTAMP WITH TIME ZONE
.虽然我不使用 Snowflake,但快速查看文档会发现它们的
TIMESTAMP_LTZ
类型类似于 SQL 标准类型TIMESTAMP WITH TIME ZONE
。
java.time.OffsetDateTime
So the appropriate class in Java is OffsetDateTime
.所以Java中合适的类是
OffsetDateTime
。
Assuming the Snowflake JDBC driver complies with JDBC 4.2 and later, you can extract an object rather than mere text from the database.假设 Snowflake JDBC 驱动程序符合 JDBC 4.2 及更高版本,您可以从数据库中提取对象而不仅仅是文本。
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
When writing to the database:写入数据库时:
myPreparedStatement.setObject( … , odt ) ;
If you want to view this moment as seen in a particular time zone, apply a ZoneId
to get a ZonedDateTime
.如果您想在特定时区查看这一时刻,请应用
ZoneId
以获取ZonedDateTime
。
ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;
You said:你说:
Sample in snowflake ltz format: 1658443006.285000000
雪花 ltz 格式的样本:1658443006.285000000
Can you cite the documentation on that?你能引用这方面的文件吗? I see no such format in their page on
TIMESTAMP_LTZ
data type.我在他们关于
TIMESTAMP_LTZ
数据类型的页面中看不到这种格式。
At any rate, I imagine the text you give represents a number of whole seconds since an epoch reference, along with a fractional second with nanosecond resolution.无论如何,我想你给出的文本代表了自纪元参考以来的整秒数,以及具有纳秒分辨率的小数秒。
Assuming the epoch reference is the first moment of 1970 as seen with an offset of zero hours-minutes-seconds from UTC (1970-01-01T00:00Z), we can easily parse that as a java.time.Instant
object in Java.假设纪元参考是 1970 年的第一个时刻,与 UTC (1970-01-01T00:00Z) 的偏移量为零时分秒,我们可以轻松地将其解析为 Java 中的
java.time.Instant
对象。 But I strongly urge you to use objects instead of this approach, as discussed above.但我强烈建议您使用对象而不是这种方法,如上所述。
Split the input into two parts, two strings.将输入分成两部分,两个字符串。 Parse each a 64-bit
long
integer number.解析每个 64 位
long
整数。
To do the split, we must escape the FULL STOP character with a pair of REVERSE SOLIDUS characters.要进行拆分,我们必须使用一对 REVERSE SOLIDUS 字符来转义 FULL STOP 字符。
String[] parts = "1658443006.285000000".split( "\\." ) ;
long seconds = Long.parseLong( parts [ 0 ] ) ;
long nanos = Long.parseLong( parts [ 1 ] ) ;
Use a factory method to instantiate a Instant
object.使用工厂方法来实例化
Instant
对象。
Instant instant = Instant.ofEpochSecond( seconds , nanos ) ;
See this code run live at Ideone.com .请参阅在 Ideone.com 上实时运行的代码。
instant.toString() → 2022-07-21T22:36:46.285Z
即时.toString() → 2022-07-21T22:36:46.285Z
An Instant
is in UTC , always in UTC, meaning an offset of zero. Instant
在UTC中,始终在 UTC 中,这意味着偏移量为零。
If you want to view this moment as seen in a particular time zone, apply a ZoneId
to get a ZonedDateTime
.如果您想在特定时区查看这一时刻,请应用
ZoneId
以获取ZonedDateTime
。
ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
zit.toString() → 2022-07-22T07:36:46.285+09:00[Asia/Tokyo]
zit.toString() → 2022-07-22T07:36:46.285+09:00[亚洲/东京]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.