這期來講一下Hive日期和時間函數,因為在一些業務的分析當中,經常要接觸到一些和時間有關的分析,例如最近幾個月,某項指標的變化情況,那么日期和時間函數還是需要掌握的。
下面這個我是按照2021-05-27日期來做的例子:
-- 當前日期:
select current_date();
結果:2021-05-27
-- 當前時間:
select current_timestamp();
結果:2021-05-27 15:54:56.052 PRC
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');
結果:2021-05-27 16:27:30
--當前時間戳:
select unix_timestamp();
結果:1622102134
--時間戳轉日期:
select from_unixtime(1622102134,'yyyy-MM-dd HH:dd:ss');
結果:2021-05-27 15:27:34
-- 昨天日期:
select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
結果:2021-05-26
-- 明天日期:
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
結果:2021-05-28
-- 前兩天日期:
select regexp_replace(to_date(date_sub(from_unixtime(unix_timestamp()),2)),'-','')
結果:2021-05-25
--兩個日期之間差值(差值為天數):
select datediff('2021-05-28','2021-05-26');
結果:2
--字符串轉時間:
select to_date('2021-05-01 11:00:12');
結果:2021-05-01
--轉換日期格式:
select date_format(current_date(),'yyyyMMdd');
結果:20210527
--utc時間轉換:
select from_utc_timestamp(current_timestamp(),'PRC');
結果:2021-05-28 00:52:07.000
--獲取秒:
select second('2021-05-27 16:52:07');
結果:7
--獲取分:
select minute('2021-05-27 16:52:07');
結果:52
--獲取時:
select hour('2021-05-27 16:52:07');
結果:16
--獲取天:
select day('2021-05-27 16:52:07');
結果:27
--獲取周:
select weekofyear('2021-05-27 16:52:07');
結果:21
--獲取月:
select month('2021-05-27 16:52:07');
結果:5
--獲取年月:
select substr(regexp_replace(to_date(from_unixtime(unix_timestamp())),'-',''),1,6);
結果:202105
--獲取季度:
select quarter('2021-05-27 16:52:07');
結果:2
--獲取年:
select year('2021-05-27 16:52:07');
結果:2021
--兩個日期相差多少小時:
select (unix_timestamp('2021-05-27 12:24:12') - unix_timestamp('2021-05-23 08:01:55'))/3600
結果:100.37
--上周第一天:
select date_add(next_day(current_timestamp(),'MO'),-7);
結果:2021-05-24
--上月第一天:
select trunc(add_months(current_timestamp(),-1),'MM');
結果:2021-04-01
--上月第一天:
select concat(substr(add_months(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1),1,7),'-01');
結果:2021-04-01
--上月最后一天:
select date_sub(trunc(current_timestamp(),'MM'),1);
結果:2021-04-30
--當月第一天:
select trunc(current_timestamp(),'MM');
結果:2021-05-01
--當月第一天:
select date_sub(current_date,dayofmonth(current_timestamp())-1);
結果:2021-05-01
--當月最后一天:
select last_day(current_timestamp());
結果:2021-05-31
--當月第幾天:
select dayofmonth(current_timestamp());
結果:27
--當年第一天:
select trunc(current_timestamp(),'yy');
結果:2021-01-01
--下周第一天:
select next_day(current_timestamp(),'MO');
結果:2021-05-31
--下月第一天:
select add_months(current_timestamp(),1)
結果:2021-06-01
--下月最后一天:
select last_day(add_months(current_timestamp(),1));
結果:2021-06-30
--上季度第一天:
select add_months(concat(year(CURRENT_DATE),'-',substr(concat('0',floor((month(CURRENT_DATE)+2)/3)*3+1),-2),'-01'),-6);
結果:2021-01-01
--本季度第一天:
select add_months(concat(year(CURRENT_DATE),'-',substr(concat('0',floor((month(CURRENT_DATE)+2)/3)*3+1),-2),'-01'),-3);
結果:2021-04-01
select to_date(concat(year(current_timestamp()),'-',lpad(ceil(month(current_timestamp())/3)*3-2,2,0),'-01'));
結果:2021-04-01
--本季度最后一天:
select last_day(to_date(concat(year(current_timestamp()),'-',lpad(ceil(month(current_timestamp())/3)*3,2,0),'-01')));
結果:2021-06-30
--字符拼接:
select concat('2019','05','11');
結果:20190511
--字符截取:
select substr('2019-05-01',0,7);
結果:2019-05
--英文日期轉換:
select from_unixtime((unix_timestamp('201509','yyyyMM')),'MMM-YY');
Sep-15
總結:unix_timestamp能夠算出時間戳,unix時間戳是從1970年1月1日(UTC/GMT的午夜)開始所經過的秒數,不考慮閏秒。from_unixtime能夠進行格式轉換,所以我們需要掌握基本的時間、日期的格式。年、季度、月、日、時、分、秒等等這些基本的函數就簡單很多。日期差datediff應該廣泛。其他的小時、分鍾等等就用時間戳相減然后除以相應的值就行。date_add和date_sub,日期的相加減,trunc截取函數較為有用。last_day、next_day、add_months對於判斷月初、月末、周初、周末這樣的非常有用。最后就是可以通過字符串的截取substr和組合concat得到我們想要的日期、時間。
關鍵詞:大數據培訓
