04-17t19:17:56.017719z格式获取当前时间戳

vdgimpew  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(379)

我正在使用hive,手头有一项重要任务,需要当前格式的时间戳 2020-04-17T19:17:56.017719Z .
任何帮助围绕这个在Hive的确切解决方案,将不胜感激。谢谢

l2osamch

l2osamch1#

with time as (
select reflect('java.util.Date','getTime') as millis
)
select concat( from_unixtime(floor(millis / 1000   ),"yyyy-MM-dd'T'HH:mm:ss"), '.',cast((millis % 1000)as int),'Z')
from time

结果:

2020-04-26T11:12:35.590Z

或者一个以上的方法

select concat(from_unixtime(unix_timestamp(split(current_timestamp,'\\.')[0]),"yyyy-MM-dd'T'HH:mm:ss"),'.',split(current_timestamp,'\\.')[1],'Z')

结果:

2020-04-26T11:28:13.433Z

使用regexp\u replace的另一个方法:

select regexp_replace(current_timestamp,'(.*) (.*)','$1T$2Z')

结果:

2020-04-26T11:50:51.804Z

如果你需要在 hive 里呆上几微秒,最坏的方法是

with time as (
select reflect('java.util.Date','getTime') as millis, reflect('java.lang.System','nanoTime') as nano 

)
select concat( from_unixtime(floor(millis / 1000   ),"yyyy-MM-dd'T'HH:mm:ss"), '.',cast(millis%1000 as int),cast(nano%1000 as int),'Z')
from time

结果:

2020-04-26T21:53:31.261356Z

但这不是真正的微秒精度。

相关问题