【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小時制

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM