hive 時間戳函數之unix_timestamp,from_unixtime


一. 日期>>>>時間戳

1.unix_timestamp() 獲取當前時間戳

例如:select unix_timestamp()   --1565858389

2.unix_timestamp(string timestame) 輸入的時間戳格式必須為'yyyy-MM-dd HH:mm:ss',如不符合則返回null

例如:
select unix_timestamp('2019-08-15 16:40:00')   --1565858400
select unix_timestamp('2019-08-15')  --null

3.unix_timestamp(string date,string pattern) 將指定時間字符串格式字符串轉化成unix時間戳,如不符合則返回null


例如:
select unix_timestamp('2019-08-15','yyyy-MM-dd')   --1565798400

select unix_timestamp('2019-08-15 16:40:00','yyyy-MM-dd HH:mm:ss')   --1565858400

select unix_timestamp('2019-08-15','yyyy-MM-dd HH:mm:ss')   --null

二. 時間戳>>>>日期

1.from_unixtime(bigint unixtime,string format) 將時間戳秒數轉化為UTC時間,並用字符串表示,可通過format規定的時間格式,指定輸出的時間格式,其中unixtime 是10位的時間戳值,而13位的所謂毫秒的是不可以的。

例如:
select from_unixtime(1565858389,'yyyy-MM-dd HH:mm:ss')  --2019-08-15 16:39:49

select from_unixtime(1565858389,'yyyy-MM-dd')   --2019-08-15

2.如果unixtime為13位的,需要先轉成10位

select from_unixtime(cast(1553184000488/1000 as int),'yyyy-MM-dd HH:mm:ss')   --2019-03-22 00:00:00

select from_unixtime(cast(substr(1553184000488,1,10) as int),'yyyy-MM-dd HH:mm:ss')  --2019-03-22 00:00:00

 三.獲取當前時間

select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')   -- 2019-08-15 17:18:55

 


免責聲明!

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



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