Hive 时间相关函数汇总
文章目录
- 时间转换
- 当前时间
- 时间加减
- 年月日
- 其他时间计算
- **求一个月的最后一天**
- **求本周一**
- **求上周一**
- **求下周一**
- **下下周一**
- **求上月一号**
- **求本月一号**
- **求下月一号**
- **hive 返回上个月第一天和最后一天**
时间转换
from_unixtime/unix_timestamp
from_unixtime
时间戳转日期函数
返回类型:string
select from_unixtime(1664553600,'yyyyMMdd') a;
20221001
unix_timestamp
返回指定日期格式的时间戳
返回类型:bigint
注意:如果后面只有date参数,date的形式必须为’yyyy-MM-dd HH:mm:ss’的形式。
返回当前时区的unix时间戳
select unix_timestamp() a;
1664553600
select unix_timestamp('20221001','yyyyMMdd') a
1664553600
select unix_timestamp('2022-10-01 11:11:10') a
1664593870
日期格式相互转换
----hive 20251001转2025-10-01
select from_unixtime(UNIX_TIMESTAMP('20251001','yyyyMMdd'),'yyyy-MM-dd') s;
----hive 2025-10-01转20251001
select from_unixtime(UNIX_TIMESTAMP('2025-10-01','yyyy-MM-dd'),'yyyyMMdd') s;
时间戳转日期格式
select from_unixtime(1759249745) s -- 2025-10-01 00:29:05
select from_unixtime(1759249745,'yyyyMMdd') z -- 20251001
select from_unixtime(1759249745,'yyyy-MM-dd') m -- 2025-10-01
日期格式转时间戳
select unix_timestamp('20251001' ,'yyyyMMdd') a ---1759248000
select unix_timestamp('2025-10-01' ,'yyyy-MM-dd') a ---1759248000
当前时间
current_date
获取当前日期
返回类型:string
select current_date;
2022-10-01
date_format
时间处理函数
select date_format('2022-10-01', 'y');
2022
select date_format('2022-10-01', 'yyyy');
2022
select date_format('2022-10-01', 'yyyy-MM');
2022-10
select date_format('2022-10-01 10:10:01', 'yyyy-MM');
2022-10
select date_format('2022-10-01', 'yyyy-MM-dd');
2022-10-01
select date_format('2022-10-01 10:13:01', 'D');
274
weekofyear
返回时间字段是本年的第多少周
返回类型:int
select weekofyear('2022-10-01 10:13:01');
39
dayofmonth
返回时间字段是本月的第多少天
返回类型:int
select dayofmonth('2022-10-01 10:03:01');
1
dayofweek
返回时间字段是本周的第多少天
返回类型:int
需要注意的是,默认上周的第一天为周日,周一为第二天,例如如下其实是周六,但是返回的是7
select dayofweek('2022-10-01');
7
select date_format('2022-10-01 10:03:01', 'EEEE')
Saturday
已知 2022-01-01是周六,如果计算的日期和2022-01-01相差再-1被7整除,说明计算日期为周日,反之则为相应的余数
SELECT IF(pmod(datediff('2022-10-01', '2022-01-01') - 1, 7)='0', 7, pmod(datediff('2022-10-01', '2022-01-01') - 1, 7))
时间加减
datediff
返回enddate与begindate之间的时间差的天数
返回类型:int
select datediff('2025-10-01','1949-10-01') a
-- 27759
date_add/date_sub
-- 返回date增加days天后的日期
-- 返回类型:string
select date_add('2025-10-01',10) a --2025-10-11
select date_add('2025-10-01',-1) a --2025-09-30
select date_sub('2025-10-01',1) a --2025-09-30
select date_add(from_unixtime(UNIX_TIMESTAMP('20251001','yyyyMMdd'),'yyyy-MM-dd'),10) b --2025-10-11
-- 返回date减少days天后的日期
-- 返回类型:string
select date_sub('2025-10-02',1)
2025-10-01
两个日期相差多少小时
select (unix_timestamp('2022-10-01 12:03:55') - unix_timestamp('2022-10-01 11:03:55'))/3600
输出:1
两个日期相差多少分钟
select (unix_timestamp('2022-10-01 12:03:55') - unix_timestamp('2022-10-01 11:03:55'))/60
输出:60
年月日
to_date
返回时间字段中的日期部分
返回类型:string
select to_date('2022-10-01 11:12:00')
2022-10-01
year
int
select year('2022-10-01 11:32:12');
输出:2022
month
返回时间字段中的月
返回类型:int
select month('2022-10-01 11:32:12');
输出:10
day
返回时间字段中的日
返回类型:int
select day('2022-10-01 11:32:12');
输出:01
hour
返回时间字段中的小时
返回类型:int
select hour('2022-10-01 11:32:12');
输出:11
minute
返回时间字段中的分钟
返回类型:int
select minute('2022-10-01 11:32:12');
输出:32
second
返回时间字段中的秒
返回类型:int
select second('2022-10-01 11:32:56');
输出:56
其他时间计算
求一个月的最后一天
select last_day(current_date);
2022-10-01
求本周一
select date_sub(next_day(to_date(CURRENT_TIMESTAMP),'MO'),7);
2022-09-26
求上周一
select date_sub(next_day(to_date(CURRENT_TIMESTAMP),'MO'),14);
2022-09-19
求下周一
select date_sub(next_day(to_date(CURRENT_TIMESTAMP),'MO'),0);
2022-10-03
下下周一
select date_add(next_day(to_date(CURRENT_TIMESTAMP),'MO'),7);
2022-10-10
求上月一号
select trunc(add_months(to_date(CURRENT_TIMESTAMP),-1),'MM');
2022-09-01
求本月一号
select trunc(to_date(CURRENT_TIMESTAMP),'MM');
2022-10-01
求下月一号
select trunc(add_months(to_date(CURRENT_TIMESTAMP),1),'MM');
2022-11-01
hive 返回上个月第一天和最后一天
select trunc(add_months(CURRENT_TIMESTAMP,-1),'MM')
select concat(substr(add_months(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1),1,7),'-01');
--上个月最后一天,先求本月1号,再取上个月最后一天
select date_sub(trunc(CURRENT_TIMESTAMP,'MM'),1);