【Hive】hive中的常⽤⽇期处理函数date_format
释义:格式化⽇期
⽤法:date_format(date,格式)
例如:
将⽇期格式化为:2020-05-01和2020-05
hive (default)> select date_format('2020-05-01 12:00:00','yyyy-MM-dd');
_c0
2020-05-01
hive (default)> select date_format('2020-05-01 12:00:00','yyyy-MM');
_c0
2020-05
date_add
释义:⽇期加法函数,数字为正,则加多少天,若数字为负数,则为减多少天;
⽤法:date_add(date,number);
例如:
将⽇期增加或减少4天;
hive (default)> select date_add('2019-05-09',4);
_c0
2019-05-13
Time taken: 0.034 seconds, Fetched: 1 row(s)
hive (default)> select date_add('2019-05-09',-4);
_c0
2019-05-05
Time taken: 0.049 seconds, Fetched: 1 row(s)
date_sub
释义:⽇期减法函数,数字为正,则减多少天,若数字为负数,则为加多少天
⽤法:date_sub(date,number)
例如:
将⽇期增加或减少4天
hive (default)> select date_sub('2019-05-09',4);
_c0
2019-05-05
Time taken: 0.04 seconds, Fetched: 1 row(s)
hive (default)> select date_sub('2019-05-09',-4);
_c0
2019-05-13
Time taken: 0.058 seconds, Fetched: 1 row(s)
next_day
释义:取该⽇期的下⼀个周⼏的⽇期
⽤法:
next_day(date,dayofweek)
星期⼀到星期⽇的英⽂(Monday,Tuesday、Wednesday、Thursday、Friday、Saturday、Sunday)
例如:
取5.1后的下⼀个周五
hive (default)> select next_day('2020-05-01',"Fri");
_c0
2020-05-08
last_day
释义:取当⽉的最后⼀天⽇期
⽤法:last_day(date)
例如:
取5⽉的最后⼀天⽇期
hive (default)> select last_day('2020-05-01');
_c0
2020-05-31
hive (default)> select last_day('2020-04-01');
_c0
2020-04-30
datediff
释义:⽇期⽐较函数,第⼀个⽇期减去第⼆个时期数字,为正,则前者⼤于后者,为负,则前者⼩于后者;
⽤法:datediff(date1,date2)
例如:
⽐较5⽉1⽇和5⽉5⽇的⼤⼩
hive (default)> select datediff('2020-05-01','2020-05-05');
_c0
-4
dayofmonth
释义:查询该⽇期在本⽉的第多少天
⽤法:dayofmonth(date)
例如:
5⽉6号在五⽉是第多少天
hive (default)> select dayofmonth('2020-05-06');
_c0
6
current_date
释义:获取当前⽇期
hive (default)> select current_date;
_c0
2020-05-14
current_timestamp
释义:获取当前时间
hive (default)> select current_timestamp;
_c0
2020-05-14 10:26:57.613
add_months
释义:⽇期加⼀个⽉
⽤法:add_months(date,number)
例如:
2020-05-03加⼀个⽉
hive (default)> select add_months('2020-05-03',1);
_c0
2020-06-03
year
释义:获取时间的年份
⽤法:year(date)
例如:
当前⽇期所在的年份
hive (default)> select year('2020-05-14 12:00:00');
_c0
2020
month
释义:获取时间的⽉份
⽤法:month(date)
例如:
当前⽇期所在的⽉份
hive (default)> select month('2020-05-14 12:00:00');
_c0
5
day
释义:获取时间的天
⽤法:day(date)
例如:
获取⽇期中的天
hive (default)> select day('2020-05-14 12:00:00');
_c0
14
hour
释义:获取时间的⼩时
⽤法:hour(time) 若时间为空,则取出值为null 例如:
获取时间中的⼩时
hive (default)> select hour('2020-05-14 12:00:00');
_c0
12
minute
释义:获取时间的分钟
⽤法:minute(time) 若时间为空,则取出值为null
例如:
获取时间中的分钟
hive (default)> select minute('2020-05-14 12:04:50');
_c0
4
second
释义:获取时间的秒
⽤法:second(time) 若时间为空,则取出值为null
例如:
获取时间中的秒
hive (default)> select second('2020-05-14 12:04:53');
_c0
53
weekofyear
js当前日期加一天
释义:⽇期所在年份的第多少周
⽤法:weekofyear(date)
例如:
当前⽇期是所在年份的第多少周
hive (default)> select weekofyear(current_date);
_c0
20
to_date
释义:转⽇期函数,默认转为yyyy-MM-dd格式
⽤法:to_date(time)
例如:
当前时间转为⽇期格式
hive (default)> select to_date(current_timestamp);
_c0
2020-05-14
常⽤⽇期需求
1.取当⽉第1天
--先获取当前⽇期在该⽉份的第n天,然后当前⽇期减去第(n-1)天,则为结果
hive (default)> select date_sub('2020-05-14',dayofmonth('2020-05-14')-1);
_c0
2020-05-01
2.取当⽉第6天
--先获取该⽇期在该⽉的第n天,然后当前⽇期减去第(n-1)天,再增加(m-1)天,则为结果
hive (default)> select date_add(date_sub('2020-05-14',dayofmonth('2020-05-14')-1),5);
_c0
2020-05-06
3.查询下⼀个⽉的第⼀天
--先获取最后⼀天,然后⽇期+1,则为下⼀⽉第⼀天
hive (default)> select date_add(last_day('2020-05-14'),1);
_c0
2020-06-01
--先获取今天是当⽉第⼏天,算出当⽉第⼀天,然后加⼀个⽉,则为下⽉第⼀天
hive (default)> select add_months(date_sub('2020-05-14',dayofmonth('2020-05-14')-1),1);
_c0
2020-06-01
4.获取当前⽇期的所在⽉份
-
-⽇期格式化⽅式
hive (default)>  select date_format('2020-05-14','MM');
_c0
05
--直接month获取⽅式
hive (default)> select month('2020-05-14');
_c0
5
5.获取本周⼀的⽇期
--先获取下周⼀的⽇期,然后减去7天,则为本周⼀⽇期
hive (default)> select date_add(next_day(current_date,"MO"),-7);
_c0
2020-04-27
6.给出⼀个⽇期计算该⽇期为周⼏
--先⼀个周⼀的⽇期,⽐如我们的2020-05-04,然后和当前⽇期(2020-05-14)做⽐较,就会得出相差的天数,再⽤pmod获取正余数的函数来获取最后的余数(0-6分别代表周⽇~周六),本实例做+1操作,直接得出周⼏的结果;hive (default)> select pmod(datediff('2020-05-14','2020-05-04') + 1,7);
_c0
4
总结
在⽇常hive计算中,最常⽤的⽇期函数有5个:date_format:常⽤于处理⽉指标
date_add或date_sub:常⽤于处理⽇指标next_day:常⽤于处理周指标
last_day:常⽤于处理⽉指标
datediff:常⽤于处理⽇指标