官网参考 :LanguageManual UDF - Apache Hive - Apache Software Foundation
常见的用法:
--string '20220715' 减去一天 '20220714'
select regexp_replace(date_sub(from_unixtime(unix_timestamp('20220715','yyyyMMdd'),'yyyy-MM-dd'),1),'-','')
select date_format(date_add(from_unixtime(unix_timestamp('20220715','yyyyMMdd'),'yyyy-MM-dd'),-1),'yyyyMMdd');
-- unix_timestamp() 将日期date转化为整型的UNIX格式的日期时间值。
-- unix_timestamp(string date)
-- unix_timestamp(string date, string pattern)
select unix_timestamp(); --1657849929
select unix_timestamp('2022-07-14 17:48:26'); --1657792106;
select unix_timestamp('20220714','yyyyMMdd'); --1657728000
-- to_date(string timestamp) 返回类型: pre 2.1.0: string 2.1.0 on: date
select to_date('2022-07-14 17:48:26'); --2022-07-14
select to_date('2022-07-14'); --2022-07-14
select to_date('20220714'); -- NULL
--from_unixtime:转化unix时间戳到当前时区的时间格式 返回类型: string
select from_unixtime(1657792106); -- 2022-07-14 17:48:26
select from_unixtime(1657792106,'yyyy-MM-dd'); -- 2022-07-14
select from_unixtime(1657792106,'yyyyMMdd'); --20220714
--date_format(date/timestamp/string ts, string fmt) 返回类型: string
select date_format('2022-07-14','yyyyMMdd'); --20220714
select date_format('2022-07-14','yyyy'); --2022
-- date_add(date/timestamp/string startdate, tinyint/smallint/int days) 返回类型: pre 2.1.0: string 2.1.0 on: date
select date_add('2022-07-31',1); --2022-08-01
select date_add('2022-07-31',-1); --2022-07-30
-- date_sub(date/timestamp/string startdate, tinyint/smallint/int days) 返回类型: pre 2.1.0: string 2.1.0 on: date
select date_sub('2022-07-31',1); --2022-07-30
select date_sub('2022-07-31',-1); --2022-08-01
--datediff(string enddate, string startdate)
select datediff('2022-07-15','2022-07-14'); -- 1
select datediff('20220715','20220714'); -- NULL
--interval
select to_date('2022-07-14') - interval 3 day; --2022-07-11 00:00:00
select current_timestamp() + INTERVAL 1 year; --2023-07-15 10:55:09.941