Hive日期時間函式學習

2021-08-11 03:10:44 字數 3523 閱讀 7560

1、日期字串與unixtime互轉

日期函式unix時間戳轉日期函式: from_unixtime

語法: from_unixtime(bigint unixtime[, string format])

返回值: string

說明: 轉化unix時間戳(從1970-01-01 00:00:00 utc到指定時間的秒數)到當前時區的時間格式

示例:

select from_unixtime(1323308943,'yyyymmdd');
獲取當前unix時間戳函式: unix_timestamp

語法: unix_timestamp()

返回值: bigint

說明: 獲得當前時區的unix時間戳

示例:

select unix_timestamp();
日期轉unix時間戳函式: unix_timestamp

語法: unix_timestamp(string date)

返回值: bigint

說明: 轉換格式為「yyyy-mm-dd hh:mm:ss「的日期到unix時間戳。如果轉化失敗,則返回0。

示例:

select unix_timestamp('2011-12-07 13:01:03');
指定格式日期轉unix時間戳函式: unix_timestamp

語法: unix_timestamp(string date, string pattern)

返回值: bigint

說明: 轉換pattern格式的日期到unix時間戳。如果轉化失敗,則返回0。

示例:

select unix_timestamp('20111207 13:01:03','yyyymmdd hh:mm:ss');
獲取當前時間戳字串: from_unixtime及unix_timestamp結合

示例:

select from_unixtime(unix_timestamp(),'yyyy-mm-dd hh:mm:ss');

2、從日期/日期時間字串取年、月、日、周、時、分、秒及日期

日期時間轉日期函式: to_date

語法: to_date(string timestamp)

返回值: string

說明: 返回日期時間欄位中的日期部分。

示例:

select to_date('2011-12-08 10:03:01');
日期轉年函式: year

語法: year(string date)

返回值: int

說明: 返回日期中的年。

示例:

select year('2011-12-08 10:03:01');

select year('2012-12-08');

日期轉月函式: month

語法: month(string date)

返回值: int

說明: 返回日期中的月份。

示例:

select month('2011-12-08 10:03:01');

select month('2011-08-08');

日期轉天函式: day

語法: day(string date)

返回值: int

說明: 返回日期中的天。

示例:

select day('2011-12-08 10:03:01');

select day('2011-12-24');

日期轉小時函式: hour

語法: hour(string date)

返回值: int

說明: 返回日期中的小時。

示例:

select hour('2011-12-08 10:03:01');
日期轉分鐘函式: minute

語法: minute(string date)

返回值: int

說明: 返回日期中的分鐘。

示例:

select minute('2011-12-08 10:03:01');
日期轉秒函式: second

語法: second(string date)

返回值: int

說明: 返回日期中的秒。

示例:

select second('2011-12-08 10:03:01');
日期轉周函式: weekofyear

語法: weekofyear (string date)

返回值: int

說明: 返回日期在當前的週數。

示例:

select weekofyear('2011-12-08 10:03:01');

3、日期天數比較及相加減

日期比較函式: datediff

語法: datediff(string enddate, string startdate)

返回值: int

說明: 返回結束日期減去開始日期的天數。

示例:

select datediff('2012-12-08','2012-05-09');
日期增加函式: date_add

語法: date_add(string startdate, int days)

返回值: string

說明: 返回開始日期startdate增加days天後的日期。

示例:

select date_add('2012-12-08',10);
日期減少函式: date_sub

語法: date_sub (string startdate, int days)

返回值: string

說明: 返回開始日期startdate減少days天後的日期。

示例:

select date_sub('2012-12-08',10);
注:不存在add_months、months_between,那些是oracle、mysql裡有的東西;如果想要在hive裡實現,需要自己另外寫udf函式。

hive日期時間函式

to date 日期時間轉日期函式 返回值 string 說明 返回日期時間欄位中的日期部分。select to date 2021 02 08 10 03 01 from dual 2021 02 08 current date 當前日期 select current date 2021 02 0...

PHP日期時間函式學習

基於指定的年份和曆法,獲取乙個月中的天數 cal days in month calendar,month,year 返回 1980 年 10 月 15 日這天是週幾 jddayofweek 函式返回日期在週幾。返回 1980 年 10 月 15 日這天的格利高里曆法的月份簡寫字串 jdmonthn...

日期函式(學習筆記)

日期函式 獲取當前系統時間 select sysdate from dual 獲取的是當前系統的時間 查詢距離今天3天後,和3天前的日期 select sysdate 今天,sysdate 3 三天前,sysdate 3 三天後 from dual 查詢出每個員工入職到今天的入職天數,以及十天前每個...