【HIVE】各种时间格式处理

yyyy-MM-dd与yyyyMMdd000000转换的三种方法

方法一:date_format(只支持yyyy-MM-dd -> yyyyMMdd000000)

select date_format('2019-10-07', 'yyyyMMdd000000')
-- 20191007000000

方法二:from_unixtime + unix_timestamp

select from_unixtime(unix_timestamp('2019-10-07', 'yyyy-MM-dd'), 'yyyyMMdd000000')
-- 20191007000000

select from_unixtime(unix_timestamp(substr('20191007000000',1,8),'yyyyMMdd'),'yyyy-MM-dd')
-- 2019-10-07

方法三:substr + concat

select concat(substr('2019-10-07',1,4),substr('2019-10-07',6,2),substr('2019-10-07',9,2),'000000')
-- 20191007000000

select concat(substr('20191007000000',1,4),'-',substr('20191007000000',5,2),'-',substr('20191007000000',7,2))
-- 2019-10-07

时间转换方法详解

unix_timestamp:格式化日期转时间戳

select unix_timestamp('2019-10-07 13:24:20','yyyy-MM-dd HH:mm:ss')
-- 1570425860

select unix_timestamp('20191007','yyyyMMdd')
-- 1570377600

from_unixtime:时间戳转格式化日期

select from_unixtime(1570425860,'yyyy-MM-dd HH:mm:ss')
-- 2019-10-07 13:24:20

select from_unixtime(1570425860,'yyyyMMdd000000')
-- 20191007000000

date_format:yyyy-MM-dd HH:mm:ss 时间转格式化时间

select date_format('2019-10-07 13:24:20', 'yyyyMMdd000000')
-- 20191007000000

select date_format('2019-10-07', 'yyyyMMdd000000')
-- 20191007000000

yyyy-MM-dd HH:mm:ss 注意

MM为月份

mm为分钟

HH为24小时制

hh为12小时制

原文地址:https://www.cnblogs.com/caizhenghui/p/11637785.html