1.取得當前日期:
select current_date(); --返回類型'yyyy-mm-dd',如今天日期'2020-01-01'
2.取得當前日期時間:
select current_timestamp(); --返回格式'yyyy-mm-dd hh:mi:ss' 如'2021-07-26 15:01:31'
3.hive取得當前時間戳:
select unix_timestamp(); --返回格式為'1627282950'
4.時間戳轉日期:
select from_unixtime(1517725479,'yyyy-MM-dd HH:dd:ss');
5.日期轉unix時間戳:
select to_nuix_timestamp('2017-01-01 12:12:12','yyyy-MM-dd HH:dd:ss');
7.hive取得當前時間:
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');
2、hive自動計算其他日期(昨天,今天):
hive中日期加減函數:date_add(start_date,num_days)
1. 取得昨天日期:
select date_add(current_date(),-1) -- 返回'2020-07-13'
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1); --返回'2020-07-13' select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1); select date_format(date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);
2. 取得明天日期:
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1); select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);
3. hive取得兩個日期之間差值(差值為天數):
datediff(date1,date2):date1大於date2,返回值為正,否則,返回值為負。
select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-10)); select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),10));
4. 字符串轉時間(字符串必須為:yyyy-MM-dd格式)
select to_date('2017-01-01 12:12:12');
5. 日期、時間戳、字符串類型格式化輸出標准時間格式:
select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss'); select date_format(current_date(),'yyyyMMdd'); select date_format('2017-01-01','yyyy-MM-dd HH:mm:ss'); --字符串必須滿足yyyy-MM-dd格式
6. utc時間轉換:
select from_utc_timestamp(current_timestamp(),8); select to_utc_timestamp(current_timestamp(),8);
二,常用日期處理-昨天、本月、上月同期、去年同期、月初、月末
以當前系統日期作為參考日期,將其轉換為統一的天維度yyyyMMdd數據格式。例如當前日期為2021-01-29
本月月初
select from_unixtime(unix_timestamp(), 'yyyyMM') 202101
上月同期
select date_format(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-1),'yyyyMMdd') 20201229
上月月初
select date_format(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-1),'yyyyMMdd') 20201229
上月月初
select CONCAT(date_format(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-1),'yyyyMM'),'01') 20201201
上月月末
select date_format(date_sub(from_unixtime(unix_timestamp(CONCAT(from_unixtime(unix_timestamp(), 'yyyyMM'),'01'), 'yyyyMMdd'), 'yyyy-MM-dd'),1),'yyyyMMdd') 20201231
去年同期
select date_format(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-12),'yyyyMMdd') 20200129
去年同期月初
select CONCAT(date_format(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-12),'yyyyMM'),'01') 20200101
二、參考日期為指定日期
指定時間格式為yyyMMdd
昨天
select date_format(date_sub(from_unixtime(unix_timestamp('20210129', 'yyyyMMdd'), 'yyyy-MM-dd'),1),'yyyyMMdd') 20210128
本月月初
select CONCAT(substr('20210129',1,6),'01') 20210101
上月同期
select date_format(add_months(from_unixtime(unix_timestamp('20210129', 'yyyyMMdd'), 'yyyy-MM-dd'),-1),'yyyyMMdd') 20201229
上月月初
select CONCAT(date_format(add_months(from_unixtime(unix_timestamp('20210129', 'yyyyMMdd'), 'yyyy-MM-dd'),-1),'yyyyMM'),'01') 20201201
上月月末
select date_format(date_sub(from_unixtime(unix_timestamp(CONCAT(from_unixtime(unix_timestamp('20210129', 'yyyyMMdd'), 'yyyyMM'),'01'), 'yyyyMMdd'), 'yyyy-MM-dd'),1),'yyyyMMdd') 20201231
去年同期
select date_format(add_months(from_unixtime(unix_timestamp('20210129', 'yyyyMMdd'), 'yyyy-MM-dd'),-12),'yyyyMMdd') 20200129
去年同期月初
select CONCAT(date_format(add_months(from_unixtime(unix_timestamp('20210129', 'yyyyMMdd'), 'yyyy-MM-dd'),-12),'yyyyMM'),'01') 20200101
參考:Hive 常用日期處理-昨天、本月、上月同期、去年同期、月初、月末等_Hive_大數據知識庫 (saoniuhuo.com)