这篇文章我们介绍下hive中的一些日期函数。
1、datediff函数
这个函数是返回结束日期减去开始日期的天数,示例如下:
datediff(string enddate, string startdate)
select datediff('2021-11-20','2021-11-22')2、date_add函数
这个函数返回开始日期startdate增加days天后的日期,示例如下:
date_add(string startdate, int days)
select date_add('2021-11-20',3)3、data_sub函数
返回开始日期startdate减少days天后的日期,示例如下:
date_sub (string startdate, int days)
select date_sub('2021-11-22',3)4、trunc函数
这个函数主要是获取指定的日期,例如:
#获取上个月1号 select trunc(add_months(current_date(),-1),'MM') #获取本月1号 select trunc(current_date(),'MM') #获取下个月1号 select trunc(add_months(current_date(),1),'MM') #获取年初日期 select trunc(current_date(),'YYYY')
下面我们列举下常用的hive中关于时间相关函数处理的案例:
1)获取当前的日期和时间
--取得当前日期: select current_date(); 输出:2021-08-14 --取得当前日期时间: select current_timestamp(); 输出:2021-08-14 13:14:57 --hive取得当前时间戳: select unix_timestamp(); 输出:1628911641
2)日期时间转日期函数,返回日期时间字段中的日期部分
-- 说明:字符串必须为:yyyy-MM-dd格式。
select to_date('2021-08-14 13:34:12');
输出:2021-08-143)时间戳转时间格式
--说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式 select from_unixtime(1323308945,’yyyy-MM-dd’); 输出:2011-12-08 select from_unixtime(1323308945,’yyyyMMdd’); 输出:20111208 --取得当前时间,相当于select current_timestamp(); select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss'); 输出:2021-08-14 03:14:57
4)日期,时间戳,字符串类型格式化输出标准时间格式
select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');
输出:2021-08-14 11:14:46
select date_format(current_date(),'yyyy-MM-dd');
输出:2021-08-14
select date_format('2021-08-14','yyyy-MM-dd HH:mm:ss');
输出:2021-08-14 00:00:005)获取当前时间的unix时间戳和日期转unix时间戳函数
select unix_timestamp();
输出:1628906623
select unix_timestamp('2021-08-14 10:05:20');
输出:16289355206)utc时间转换:
select from_utc_timestamp(current_timestamp(),8); 输出:2021-08-14 11:10:27.762 select to_utc_timestamp(current_timestamp(),8); 输出:2021-08-14 11:10:56.085
7)日期转unix时间戳
select to_unix_timestamp('2021-08-14 11:10:27','yyyy-MM-dd HH:dd:ss');
输出:16285932278)返回日期中的年/季度/月/日
select year('2021-08-14 10:05:20');
输出:2021
select quarter('2021-08-14 10:05:20');
输出:3
select month('2021-08-14 10:05:20');
输出:8
select day('2021-08-14 10:05:20');
输出:149)返回日期中的时/分/秒
select hour('2021-08-14 10:05:20');
输出:10
select minute('2021-08-14 10:05:20');
输出:5
select second('2021-08-14 10:05:20');
输出:2010)返回日期在当年的第几周
select weekofyear('2021-08-14 10:05:20');
输出:3211)返回日期在当前周的第几天(注:周日为第一天)
select dayofweek('2021-08-14 10:05:20'); --周六
输出:712)日期比较函数,返回开始日期减去结束日期的天数
说明:前者大于后者,返回值为正,否则,返回值为负。
select datediff('2021-08-14','2021-08-08');
输出:6
select datediff(current_date(),date_add(current_date(),-10));
输出:10
select datediff(current_date(),date_add(current_date(),10));
输出:-1013)日期减少函数,返回日期前N天的日期
select date_sub('2021-08-14',6);
输出:2021-08-0814)日期增加函数,返回日期后n天的日期
select date_add('2021-08-14',6);
输出:2021-08-20
--取得昨天日期:
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);
select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
-- 取得明天日期:
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);15、返回两个日期之间包含的月数(结果为double类型)
select months_between('2021-10-14','2021-05-04');
输出:5.3225806516、获取日期月初(参数mm),年初日期(参数yy)
select trunc(current_date(),'MM'); 输出:2021-08-01 select trunc(current_date(),'YY'); 输出:2021-01-01
17、获取日期当月最后一天
select last_day(current_date()); 输出:2021-08-31
18、返回当前日期之后的下一个星期几的日期
说明:参数为MO,TU,WE,TH,FR,SA,SU,也可以是两个缩写字母可以是三个可以是全名。
select next_day('2021-08-14', 'TU'); --得到021-08-14后的下个周二
输出:2021-08-17
select next_date('2020-01-01','Fri'); --得到2020-01-01后的下个周五
输出:2020-01-0319、yyyymmdd和yyyy-mm-dd日期间切换
思路:先转换成时间戳,再由时间戳转换为对应格式。
select from_unixtime(unix_timestamp('20210814','yyyymmdd'),'yyyy-mm-dd');
输出:2021-08-14
select from_unixtime(unix_timestamp('2021-08-14','yyyy-mm-dd'),'yyyymmdd') ;
输出:2021081420、取最近30天的数据
select * from table where datediff(current_timestamp(),create_time)<=30; --要从表中运行
21、两个日期相差多少小时
select (unix_timestamp('2021-08-14 10:18:54')-unix_timestamp('2021-08-14 08:18:54'))/3600;
输出:2.022、两个日期相差多少分钟
select (unix_timestamp('2021-08-14 10:18:54')-unix_timestamp('2021-08-14 08:18:54'))/60
输出:120.023、返回上个月第一天和最后一天
--上个月第一天 select trunc(add_months(current_timestamp(),-1),'MM'); select concat(substr(add_months(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1),1,7),'-01'); select trunc(add_months(current_timestamp(),-1),'MM'); 输出:2021-07-01 --上个月最后一天 select last_day(add_months(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1)) ; select date_sub(trunc(current_timestamp(),'MM'),1); 输出:2021-07-31 -- 获取当月第一天 select trunc(current_timestamp(),'MM'); select from_unixtime(unix_timestamp(),'yyyy-MM-01'); 输出:2021-08-01 -- 获取当月最后一天 select last_day(current_timestamp()); select last_day(current_date()); 输出:2021-08-31

还没有评论,来说两句吧...