如何转换"2019-11-02T20:18:00Z"在HQL中加盖时间戳? [英] How to convert "2019-11-02T20:18:00Z" to timestamp in HQL?

查看:74
本文介绍了如何转换"2019-11-02T20:18:00Z"在HQL中加盖时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有日期时间字符串"2019-11-02T20:18:00Z" .如何在Hive HQL中将其转换为时间戳?

I have datetime string "2019-11-02T20:18:00Z". How can I convert it into timestamp in Hive HQL?

推荐答案

如果要保留毫秒数,然后删除 Z ,将 T 替换为空格并转换为时间戳:/p>

If you want preserve milliseconds then remove Z, replace T with space and convert to timestamp:

select timestamp(regexp_replace("2019-11-02T20:18:00Z", '^(.+?)T(.+?)Z$','$1 $2'));

结果:

2019-11-02 20:18:00

它还可以使用毫秒:

 select timestamp(regexp_replace("2019-11-02T20:18:00.123Z", '^(.+?)T(.+?)Z$','$1 $2'));

结果:

2019-11-02 20:18:00.123

使用 from_unixtime(unix_timestamp())解决方案无法使用毫秒.演示:

Using from_unixtime(unix_timestamp()) solution does not work with milliseconds. Demo:

  select from_unixtime(unix_timestamp("2019-11-02T20:18:00.123Z", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));

结果:

2019-11-02 20:18:00

毫秒丢失.原因是 函数unix_timestamp返回秒是从UNIX时代(1970-01-01 00:00:00 UTC)传递过来的..

Milliseconds are lost. And the reason is that function unix_timestamp returns seconds passed from the UNIX epoch (1970-01-01 00:00:00 UTC).

这篇关于如何转换"2019-11-02T20:18:00Z"在HQL中加盖时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆