hive 中時間函式

2021-09-24 17:58:46 字數 2557 閱讀 5151

to_date:日期時間轉日期函式

select to_date('2015-04-02 13:34:12');

輸出:2015-04-02

## 當前日期和時間

select current_timestamp();

-- 2018-04-28 11:46:03.136

## 獲取當前日期,當前是 2018-04-28

select current_date;

orselect current_date();

-- 2018-04-28

## 獲取unix系統下的時間戳

select unix_timestamp();

-- 1524884881

## 當前是 2018-04-28

select substr(current_timestamp, 0, 10);

-- 2018-04-28

## 當前是 2018-04-28

select date_sub(current_date, 1);

-- 2018-04-27

## yyyy-mm-dd hh:mm:ss 擷取日期

select to_date("2017-10-22 10:10:10");

-- 2017-10-22

## 兩個日期之間的天數差

select datediff("2017-10-22", "2017-10-12");

-- 10

select datediff("2017-10-22 10:10:10", "2017-10-12 23:10:10");

-- 10

select datediff("2017-10-22 01:10:10", "2017-10-12 23:10:10");

-- 10

## 時間擷取

select from_unixtime(cast(substr("1504684212155", 0,10) as int)) dt;

-- 2017-09-06 15:50:12

## 時間戳轉日期

## 語法: to_date(string timestamp)

select to_date(from_unixtime(unix_timestamp()));

-- 2018-04-28

select from_unixtime(unix_timestamp(),'yyyy-mm-dd 10:30:00');

-- 2018-04-28 10:30:00

select concat(date_sub(current_date,1),' 20:30:00');

-- 2018-04-27 20:30:00

-- hive version 1.2.0

select date_format(date_sub(current_date,1),'yyyy-mm-dd 20:30:00');

from_unixtime:轉化unix時間戳到當前時區的時間格式

select from_unixtime(1323308943,』yyyymmdd』);

輸出:20111208

unix_timestamp:獲取當前unix時間戳

select unix_timestamp();

輸出:1430816254

select unix_timestamp('2015-04-30 13:51:20');

輸出:1430373080

year:返回日期中的年

select year('2015-04-02 11:32:12');

輸出:2015

month:返回日期中的月份

select month('2015-12-02 11:32:12');

輸出:12

day:返回日期中的天

select day('2015-04-13 11:32:12');

輸出:13

hour:返回日期中的小時

select hour('2015-04-13 11:32:12');

輸出:11

minute:返回日期中的分鐘

select minute('2015-04-13 11:32:12');

輸出:32

second:返回日期中的秒

select second('2015-04-13 11:32:56');

輸出:56

weekofyear:返回日期在當前週數

select weekofyear('2015-05-05 12:11:1');

輸出:19

datediff:返回開始日期減去結束日期的天數

select datediff('2015-04-09','2015-04-01');

輸出:8

date_sub:返回日期前n天的日期

select date_sub('2015-04-09',4);

輸出:2015-04-05

date_add:返回日期後n天的日期

select date_add('2015-04-09',4);

輸出:2015-04-13

Hive 時間函式

to date 日期時間轉日期函式select to date 2015 04 02 13 34 12 輸出 2015 04 02from unixtime 轉化unix時間戳到當前時區的時間格式select from unixtime 1323308943,yyyymmdd 輸出 20111208...

hive時間函式

語法 from unixtime bigint unixtime stringformat 返回值 string 說明 轉化unix時間戳 從1970 01 0100 00 00 utc到指定時間的秒數 到當前時區的時間格式 舉例 select from unixtime 1323308943,yy...

hive中的時間處理函式

返回值 string 說明 轉化unix時間戳 從1970 01 01 00 00 00 utc到指定時間的秒數 到當前時區的時間格式 舉例 hive select from unixtime 1323308943,yyyymmdd from dual 20111208 獲取當前unix時間戳函式 ...