1. 程式人生 > 其它 >Hive 日期時間相關函式總結

Hive 日期時間相關函式總結

Hive 日期時間相關函式

版本環境:hive-2.1.1

一、日期獲取

current_date() 獲取當前日期

select current_date(); --返回型別'yyyy-mm-dd'

current_timestamp() 獲取當前日期時間

select current_timestamp();  --返回格式'yyyy-mm-dd hh:mi:ss'

unix_timestamp() 取得當前時間戳、指定日期時間戳

select unix_timestamp();
select unix_timestamp('20220324', 'yyyyMMdd'); --返回20220324的時間戳

from_unixtime(時間戳,日期格式) 時間戳轉換

select from_unixtime(1648103561,'yyyy-MM-dd HH:dd:ss'); -- 後面可以填想要的日期格式不侷限於舉的例子
select from_unixtime(1648103561,'yyyy-MM-dd');


to_nuix_timestamp(日期時間,日期時間格式) 日期時間轉時間戳

select to_unix_timestamp('2022-03-24 14:24:41','yyyy-MM-dd HH:dd:ss');
select to_unix_timestamp('2022-03-24 14:24:41','yyyy-MM-dd'); -- 會按照傳入的格式去擷取,傳入的格式不對結果會異常


取得當前時間 from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss')

select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');

二、日期計算相關函式

date_add(日期時間,nd) 日期加減(按日),傳入日期格式需要為yyyy-MM-dd可搭配其他函式一起食用

select date_add('2022-03-24',-1); -- 返回2022-03-23
select date_add('2022-03-24',5);  -- 返回2022-03-29
select date_add(from_unixtime(unix_timestamp('20220324','yyyyMMdd'),'yyyy-MM-dd'),-1); -- 返回2022-03-23
select date_add(from_unixtime(unix_timestamp('20220324','yyyyMMdd'),'yyyy-MM-dd'),5); -- 返回2022-03-29

add_months(日期時間,nm) 日期加減(按月),傳入日期格式需要為yyyy-MM-dd可搭配其他函式一起食用

select add_months('2022-03-28',1); -- 返回 2022-04-28
select add_months('2022-02-28',1); -- 返回 2022-03-31 返回的不是03-28這個需要注意
select add_months('2022-04-30',-1); -- 返回 2022-03-31 這種情況也需要注意


next_day(引數1,引數2) 返回下一個星期的某一天,具體看例子

引數1:引數1為yyyy-MM-dd格式的日期,如果為yyyy-MM-dd HH:mm:ss的格式會擷取掉時間,建議搭配其他日期函式轉換成yyyy-MM-dd
引數2:引數2為週一到週日的英文字串,可以是簡寫Monday、Tuesday、Wednesday、Thursday、Friday、Saturday、Sunday

-- 3-24 為週四
select next_day('2022-03-24','Mon'); -- 返回下一個週一的日期 2022-03-28
select next_day('2022-03-24','Sun'); -- 返回下一個週日(本週日) 2022-03-27
select next_day('2022-03-23','Fri'); -- 返回 2022-03-25
select next_day('2022-03-24','Fri'); -- 返回 2022-03-25

last_day(日期時間) 返回當月最後一天,日期格式需要為yyyy-MM-dd

select last_day('2022-03-24'); -- 返回 2022-03-31

date_sub(startdate,days) 返回startdate減去days天數的日期。返回VARCHAR型別的yyyy-MM-dd日期格式。若有引數為null或解析錯誤,返回null

select date_sub('2022-03-24',24); -- 返回 2022-02-28
select date_sub('2022-03-24',-8); -- 返回 2022-04-01

trunc(date, fmt) 為指定元素而截去的日期值 我測試的時候在hive裡fmt引數必需要大寫,Oracle裡大小寫都行,而且引數型別也沒Oracle豐富,參考文章寫的是hive,不排除版本的原因

date 日期時間
fmt 指定的元素擷取格式

select trunc('2022-03-24','MM'); -- 返回date當月第一天 2022-03-01
select trunc('2022-03-24','YY'); -- 返回date當年第一天 2022-01-01
select trunc('2022-03-24','YYYY'); -- 返回date當年第一天 2022-01-01

datediff(date1,date2) 取得兩個日期之間差值(差值為天數)date1-date2,日期格式需要為yyyy-MM-dd

select datediff('2022-03-24','2022-03-20'); -- 返回值 4
select datediff('2022-03-24','2022-03-30'); -- 返回值 -6

三、格式轉換

to_date()字串轉date型別(字串必須為:yyyy-MM-dd格式)

select to_date('2022-03-24 15:36:36'); -- 返回2022-03-24

date_format()日期、時間戳、字串型別格式化輸出標準時間格式

select date_format('2022-03-24','yyyy年MM月dd日 HH:mm:ss'); -- 可以格式化成想要的效果,第一個引數需要滿足滿足yyyy-MM-dd格式

四、常用日期處理(總之不斷的套娃就能得到你想要的結果,處理方法可能不止文中的一種)

下文的例子輸入日期格式和輸出日期格式都為yyyyMMdd,根據你的需要調整

昨天

select date_format(date_sub(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),1),'yyyyMMdd'); -- 返回 20220323
select date_format(date_add(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),-1),'yyyyMMdd'); -- 返回 20220323

本月初

select date_format(trunc(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),'MM'),'yyyyMMdd'); -- 返回 20220301

本月底

select date_format(last_day(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd')),'yyyyMMdd'); -- 返回 20220331

上月初

select date_format(trunc(add_months(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),-1),'MM'),'yyyyMMdd'); -- 返回 20220201

上月底

select date_format(last_day(add_months(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),-1)),'yyyyMMdd'); -- 返回 20220228

去年同期

select date_format(add_months(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),-12),'yyyyMMdd');

本週一

select date_format(next_day(date_add(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),-7),'Mon'),'yyyyMMdd'); -- 返回 20220321

本週日

select date_format(date_add(next_day(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),'Mon'),-1),'yyyyMMdd'); -- 返回 20220327

上週一

select date_format(next_day(date_add(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),-14),'Mon'),'yyyyMMdd'); -- 返回 20220314

上週日

select date_format(date_add(next_day(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),'Mon'),-8),'yyyyMMdd'); -- 返回 20220320

本季度第一天

select date_format(to_date(concat(
                 substring('20220324', 1, 4),
                 CASE floor(cast(substring('20220324', 5, 2) AS double) / 3.1) + 1
                     WHEN 1 THEN "-01-01"
                     WHEN 2 THEN "-04-01"
                     WHEN 3 THEN "-07-01"
                     WHEN 4 THEN "-10-01" END
             )), 'yyyyMMdd'); -- 返回 20220101

去年同季度第一天

select date_format(add_months(to_date(concat(
                 substring('20220324', 1, 4),
                 CASE floor(cast(substring('20220324', 5, 2) AS double) / 3.1) + 1
                     WHEN 1 THEN "-01-01"
                     WHEN 2 THEN "-04-01"
                     WHEN 3 THEN "-07-01"
                     WHEN 4 THEN "-10-01" END
             )), -12), 'yyyyMMdd'); -- 返回 20210101

本年初

select date_format(trunc(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),'YY'),'yyyyMMdd'); -- 返回 20220101

本年底

select date_format(date_add(add_months(trunc(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),'YY'),12),-1),'yyyyMMdd'); -- 返回 20221231

去年初

select date_format(add_months(trunc(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),'YY'),-12),'yyyyMMdd'); -- 返回 20210101

去年底

select date_format(date_add(trunc(from_unixtime(unix_timestamp('20220324', 'yyyyMMdd'),'yyyy-MM-dd'),'YY'),-1),'yyyyMMdd'); -- 返回 20211231

五、總結

先記錄這些以後遇到了在補吧,這些函式在工作中應該夠用了,多套娃吧。