hive日期時間函式

2021-10-19 07:45:45 字數 3754 閱讀 9874

to_date():日期時間轉日期函式:

返回值: string

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

select to_date(』2021-02-08 10:03:01′) from dual;

2021-02-08

current_date():當前日期

select current_date() --2021-02-09

current_timestamp():當前日期時間

select current_timestamp() ; --2021-02-09 11:43:00

from_unixtime():轉化unix時間戳到當前時區的時間格式(格式中的m必須大寫)

返回值: string

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

select from_unixtime(1565776746,'yyyymmdd'); -'20190814'

select from_unixtime(1565776746,'yyyy-mm-dd'); -'2019-08-14'

select from_unixtime(1612838819),from_unixtime(unix_timestamp()); --2021-02-09 10:46:44     2021-02-09 10:46:59

select from_unixtime(1565776746,'yyyy-mm-dd hh:mm:ss'); -'2019-08-14 17:59:06'

unix_timestamp():獲取當前unix時間戳函式

unix_timestamp(string date):日期轉unix時間戳函式

unix_timestamp(string date, string pattern):指定格式日期轉unix時間戳函式

返回值: bigint

select unix_timestamp(),unix_timestamp('2019-04-30 13:51:20'); --1430816254  1556603480

應用:yyyymmdd與yyyy-mm-dd格式轉換

select unix_timestamp('20171205','yyyymmdd'); --1483546320

select from_unixtime(unix_timestamp('20171205','yyyymmdd'),'yyyy-mm-dd') --20171205轉成2017-12-05

select from_unixtime(unix_timestamp('2017-12-05','yyyy-mm-dd'),'yyyymmdd') from dual; --2017-12-05轉成20171205

year():返回日期/時間中的年

返回值: int

select year('2021-02-08 10:03:01') --2021

quarter():返回日期/時間的季度

select quarter('2021-02-09'), quarter('2021-04-09 12:12:01') --1  2

month():返回日期/時間中的月

返回值: int

select month('2021-02-08 10:03:01') --2

day():返回日期/時間中的天

返回值: int

select day('2021-02-14 13:14:12'); --14

hour():返回日期/時間中的小時

返回值: int

select hour('2021-01-13 11:32:12'); --11

minute():返回日期/時間中的分鐘

返回值: int

select minute('2021-01-14 13:14:12'); --14

second():返回日期/時間中的秒

返回值: int

select minute('2021-01-14 13:14:12'); --12

last_day(): 返回日期/時間月末日期

select last_day('2021-02-02 12:34:34'); --2021-02-28

next_day():返回當前時間的下乙個星期x所對應的日期

select next_day('2020-11-05','sunday') --返回下乙個週日

select next_day('2021-02-09', 'tu') --返回下乙個周二 2021-02-16

datediff():返回開始日期/時間減去結束日期/時間的天數

select datediff('2019-04-09','2019-04-01'); --8

date_sub():返回日期/時間前n天的日期

select date_sub('2019-04-09',4),date_sub('2019-04-09 13:51:20',4); --2019-04-05      2019-04-05

date_add():返回日期/時間後n天的日期

select date_add('2019-04-09',4); --'2019-04-13'

add_months(): 返回日期/時間後n月的日期

select add_months('2019-04-09 12:34:33',4); --'2019-08-09'

weekofyear():返回日期/時間在當前年第幾周

select weekofyear('2019-01-09 12:11:01'); --當年中的第2周

dayofmonth(): 返回日期/時間在當前月第幾天

select dayofmonth('2019-01-09 12:11:01'); --當年中的第9天

Hive日期時間函式學習

1 日期字串與unixtime互轉 日期函式unix時間戳轉日期函式 from unixtime 語法 from unixtime bigint unixtime string format 返回值 string 說明 轉化unix時間戳 從1970 01 01 00 00 00 utc到指定時間的...

Hive日期函式

hive日期函式 函式用法 含參方式 用法備註 hive日期函式 函式用法 含參方式 用法備註 date2datekey date格式轉換成datekey date2datekey string date time date2datekey 2017 09 01 返回 20170901 date a...

Hive日期函式

1 unix timestamp 2018 12 05 08 45 17 作用 統計從1970年開始到現在的秒數 2 from unixtime 1525509615,yyyymmdd 作用 日期函式unix時間戳轉日期函式 3 to date 2018 12 08 10 08 01 作用 返回日期...