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
select trunc('2022-03-24','YEAR'); -- 返回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
五、總結
先記錄這些以后遇到了在補吧,這些函數在工作中應該夠用了,多套娃吧。