hive的函数有很多,今天我带大家来总结下hive中常用的日期函数吧!!!
如有不足,还请大家多多指出,希望能和大家一起交流,共同进步!
时间一点一滴在溜走,我们要珍惜每一刻
1.日期时间转日期函数:to_date()
to_date(string timestamp) 返回日期时间字段中的日期部分。
返回类型:string
select to_date('-12-08 10:03:01') ---12-08
2.获取当前日期:current_date()
current_date() 返回当前时间日期
返回类型:date
select current_date() ---02-14
3.查询当前系统时间(包括毫秒数): current_timestamp()
current_timestamp() 返回当前时间戳
返回类型:timestamp
select current_timestamp() ---02-14 18:50:50.241
4.日期增加函数:date_add()
date_add(stringstartdate,intdays) 返回开始日期startdate增加days天后的日期
返回类型:string
select date_add('-12-31', 1) ---01-01
5.日期减少函数:date_sub()
date_sub (stringstartdate,intdays) 返回开始日期startdate减少days天后的日期
返回类型:string
select date_sub('-12-31', 1) ---12-30
6.日期比较函数:datediff()
datediff(stringenddate,stringstartdate) 返回结束日期减去开始日期的天数
返回类型:int
select datediff('-02-15','-02-01') --14
7.日期格式化,按照格式返回字符串:date_format()
date_format(date/timestamp/string, string fmt) 按指定格式返回date
返回类型: string
select date_format('-04-08 12:12:12','yyyy-MM-dd') ---04-08select date_format('-04-08 12:12:12','yyyyMMdd') --0408select date_format('-04-08 12:12:12','yyyy-MM') ---04select date_format('-04-08 12:12:12','yyyy') --
8.日期转年函数:year()
year(string/date) 返回时间字符串的年份部分
返回类型:int
select year('-04-08 12:12:12') --
9.月份函数:month()
month(string/date) 返回时间字符串的月份
返回类型:int
select month('-04-11') --4
10.天函数:day() /dayofmonth(date)
day(string/date) 返回时间字符串的天
返回类型:int
select day('-04-08 12:12:12') --8select day('-04-11') --11select dayofmonth('-04-08 12:13:11') --8
11.小时函数:hour()
hour(string/date) 返回时间字符串的小时数字
返回类型:int
select hour('-04-08 12:13:11') --12
12.分钟函数:minute()
minute(string/date) 返回时间字符串的分钟数字
返回类型:int
select minute('-04-08 12:13:11') --13
13.秒函数:second()
second(string/date) 返回时间字符串的分钟数字
返回类型:int
select second('-04-08 12:13:11') --11
14.月份差:months_between()
months_between(date1, date2) 返回date1与date2之间相差的月份,如date1>date2,则返回正,如果date1
返回类型:double
select months_between('-02-25','-01-26') --0.96774194select months_between('-02-25','-02-26') --0.03225806select months_between('-02-25','-02-25') --0
15.增加月份:add_months()
add_months(string start_date, int num_months) 返回当前时间下再增加num_months个月的日期
返回类型:string
select add_months('-01-01',2) ---03-01
16.查询时间字符串位于一年中的第几个周内:weekofyear()
weekofyear(string/date) 返回时间字符串位于一年中的第几个周内
返回类型:int
select weekofyear('-04-08 12:13:11') --15
17.查询当月第几天:dayofmonth(current_date)
返回类型:int
select dayofmonth(current_date()) --14
18.返回月末: last_day()
last_day(string date) 返回这个月的最后一天的日期,忽略时分秒部分(HH:mm:ss)
返回类型:string
select last_day(current_date()) ---02-28
19.返回时间的最开始年份或月份 :trunc()
trunc(string date, string format)返回时间的最开始年份或月份
返回类型:string
select trunc('-04-08','YY') ---01-01select trunc('-04-08','MM') ---04-01
诗和远方
20.返回当月第1天:
有多种方法可以灵活使用,这里我列出我经常使用的两种方法
1).trunc(current_timestamp(),'MM')
select trunc(current_timestamp(),'MM') -----02-01
2).date_sub(current_date,dayofmonth(current_date)-1)
select date_sub(current_date,dayofmonth(current_date)-1) ---02-01
21.返回下个月第1天:
1).trunc(add_months(current_timestamp(),1),'MM')
select trunc(add_months(current_timestamp(),1),'MM') ---03-01
2). add_months(date_sub(current_date,dayofmonth(current_date)-1),1)
select add_months(date_sub(current_date,dayofmonth(current_date)-1),1) ---03-01
22.返回上个月第一天:
1). trunc(add_months(current_timestamp(),-1),'MM')
select trunc(add_months(current_timestamp(),-1),'MM') ---01-01
2). add_months(date_sub(current_date,dayofmonth(current_date)-1),-1)
select add_months(date_sub(current_date,dayofmonth(current_date)-1),-1) ---01-01
23. 下周几的具体日期: next_day()
next_day(string date, string week) 返回当前时间的下一个星期X所对应的日期
返回类型:string
下周一:select next_day(to_date(CURRENT_TIMESTAMP),'MO') ---02-18本周一:select date_sub(next_day(to_date(CURRENT_TIMESTAMP),'MO'),7) ---02-11上周一:select date_sub(next_day(to_date(CURRENT_TIMESTAMP),'MO'),14) ---02-04
工作之余,小酌一杯吧
24.UNIX时间戳转日期函数:from_unixtime()
from_unixtime(bigint unixtime[, string format]) 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式
返回类型:string
select from_unixtime(1323308143,'yyyy-MM-dd') ---12-08
25.获取当前UNIX时间戳函数: unix_timestamp()
返回类型: bigint
select unix_timestamp() --1554721853
26.日期转UNIX时间戳函数: unix_timestamp()
unix_timestamp(string date) 转换格式为“yyyy-MM-dd HH:mm:ss“的日期到UNIX时间戳。如果转化失败,则返回0
返回类型: bigint
select unix_timestamp('-03-07 13:01:03') --1551934863
27.指定格式日期转UNIX时间戳函数: unix_timestamp
unix_timestamp(string date, string pattern) 转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0
返回类型: bigint
unix_timestamp('-03-20', 'yyyy-MM-dd') --1553011200
28.hive中获取当前时间
from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')