hive日期函數
hive的函數有很多,今天我帶大家來總結下hive中常用的日期函數吧!!!
如有不足,還請大家多多指出,希望能和大家一起交流,共同進步!
1.日期時間轉日期函數:to_date()
to_date(string timestamp) 返回日期時間字段中的日期部分。
返回類型:string
select to_date('2011-12-08 10:03:01') --2011-12-08
2.獲取當前日期:current_date()
current_date() 返回當前時間日期
返回類型:date
select current_date() --2019-02-14
3.查詢當前系統時間(包括毫秒數): current_timestamp()
current_timestamp() 返回當前時間戳
返回類型:timestamp
select current_timestamp() --2019-02-14 18:50:50.241
4.日期增加函數:date_add()
date_add(string startdate, int days) 返回開始日期startdate增加days天后的日期
返回類型:string
select date_add('2018-12-31', 1) --2019-01-01
5.日期減少函數:date_sub()
date_sub (string startdate, int days) 返回開始日期startdate減少days天后的日期
返回類型:string
select date_sub('2018-12-31', 1) --2018-12-30
6.日期比較函數:datediff()
datediff(string enddate, string startdate) 返回結束日期減去開始日期的天數
返回類型:int
select datediff('2019-02-15','2019-02-01') --14
7.日期格式化,按照格式返回字符串:date_format()
date_format(date/timestamp/string, string fmt) 按指定格式返回date
返回類型: string
select date_format('2019-04-08 12:12:12','yyyy-MM-dd') --2019-04-08
select date_format('2019-04-08 12:12:12','yyyyMMdd') --20190408
select date_format('2019-04-08 12:12:12','yyyy-MM') --2019-04
select date_format('2019-04-08 12:12:12','yyyy') --2019
8.日期轉年函數:year()
year(string/date) 返回時間字符串的年份部分
返回類型:int
select year('2019-04-08 12:12:12') --2019
9.月份函數:month()
month(string/date) 返回時間字符串的月份
返回類型:int
select month('2019-04-11') --4
10.天函數:day() /dayofmonth(date)
day(string/date) 返回時間字符串的天
返回類型:int
select day('2019-04-08 12:12:12') --8
select day('2019-04-11') --11
select dayofmonth('2019-04-08 12:13:11') --8
11.小時函數:hour()
hour(string/date) 返回時間字符串的小時數字
返回類型:int
select hour('2019-04-08 12:13:11') --12
12.分鐘函數:minute()
minute(string/date) 返回時間字符串的分鐘數字
返回類型:int
select minute('2019-04-08 12:13:11') --13
13.秒函數:second()
second(string/date) 返回時間字符串的分鐘數字
返回類型:int
select second('2019-04-08 12:13:11') --11
14.月份差:months_between()
months_between(date1, date2) 返回date1與date2之間相差的月份,如date1>date2,則返回正,如果date1<date2,則返回負,否則返回0.0
返回類型:double
select months_between('2019-02-25','2019-01-26') --0.96774194
select months_between('2019-02-25','2019-02-26') --0.03225806
select months_between('2019-02-25','2019-02-25') --0
15.增加月份:add_months()
add_months(string start_date, int num_months) 返回當前時間下再增加num_months個月的日期
返回類型:string
select add_months('2019-01-01',2) --2019-03-01
16.查詢時間字符串位於一年中的第幾個周內:weekofyear()
weekofyear(string/date) 返回時間字符串位於一年中的第幾個周內
返回類型:int
select weekofyear('2019-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()) --2019-02-28
19.返回時間的最開始年份或月份 :trunc()
trunc(string date, string format) 返回時間的最開始年份或月份
返回類型:string
select trunc('2019-04-08','YY') --2019-01-01
select trunc('2019-04-08','MM') --2019-04-01
20.返回當月第1天:
有多種方法可以靈活使用,這裡我列出我經常使用的兩種方法
1).trunc(current_timestamp(),'MM')
select trunc(current_timestamp(),'MM') ----2019-02-01
2).date_sub(current_date,dayofmonth(current_date)-1)
select date_sub(current_date,dayofmonth(current_date)-1) --2019-02-01
21.返回下個月第1天:
1).trunc(add_months(current_timestamp(),1),'MM')
select trunc(add_months(current_timestamp(),1),'MM') --2019-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) --2019-03-01
22.返回上個月第一天:
1). trunc(add_months(current_timestamp(),-1),'MM')
select trunc(add_months(current_timestamp(),-1),'MM') --2019-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) --2019-01-01
23. 下週幾的具體日期: next_day()
next_day(string date, string week) 返回當前時間的下一個星期X所對應的日期
返回類型:string
下週一:select next_day(to_date(CURRENT_TIMESTAMP),'MO') --2019-02-18
本週一:select date_sub(next_day(to_date(CURRENT_TIMESTAMP),'MO'),7) --2019-02-11
上週一:select date_sub(next_day(to_date(CURRENT_TIMESTAMP),'MO'),14) --2019-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') --2011-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('2019-03-07 13:01:03') --1551934863
27.指定格式日期轉UNIX時間戳函數: unix_timestamp
unix_timestamp(string date, string pattern) 轉換pattern格式的日期到UNIX時間戳。如果轉化失敗,則返回0
返回類型: bigint
unix_timestamp('2009-03-20', 'yyyy-MM-dd') --1553011200
28.hive中獲取當前時間
from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')