Hive中日期函數總結


--Hive中日期函數總結:
--1.時間戳函數
--日期轉時間戳:從1970-01-01 00:00:00 UTC到指定時間的秒數
select unix_timestamp(); --獲得當前時區的UNIX時間戳
select unix_timestamp('2017-09-15 14:23:00');
select unix_timestamp('2017-09-15 14:23:00','yyyy-MM-dd HH:mm:ss');
select unix_timestamp('20170915 14:23:00','yyyyMMdd HH:mm:ss');

--時間戳轉日期
select from_unixtime(1505456567);
select from_unixtime(1505456567,'yyyyMMdd');
select from_unixtime(1505456567,'yyyy-MM-dd HH:mm:ss');
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss'); --獲取系統當前時間

--2.獲取當前日期: current_date
hive> select current_date from dual
2017-09-15

--3.日期時間轉日期:to_date(string timestamp)
hive> select to_date('2017-09-15 11:12:00') from dual;
2017-09-15

--3.獲取日期中的年/月/日/時/分/秒/周
with dtime as(select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') as dt)
select year(dt),month(dt),day(dt),hour(dt),minute(dt),second(dt),weekofyear(dt)
from dtime

--4.計算兩個日期之間的天數: datediff
hive> select datediff('2017-09-15','2017-09-01') from dual;
14

--5.日期增加和減少: date_add/date_sub(string startdate,int days)
hive> select date_add('2017-09-15',1) from dual;
2017-09-16
hive> select date_sub('2017-09-15',1) from dual;
2017-09-14

--其他日期函數
查詢當前系統時間(包括毫秒數): current_timestamp;
查詢當月第幾天: dayofmonth(current_date);
月末: last_day(current_date)
當月第1天: date_sub(current_date,dayofmonth(current_date)-1)
下個月第1天: add_months(date_sub(current_date,dayofmonth(current_date)-1),1)

-- 獲取下一個月
add_months(current_date,1)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM