Hive 時間轉換
UNIX時間戳概念:因爲UNIX時間戳只是一個秒數,一個UNIX時間戳在不同時區看來,時間是不同的。
如UNIX時間戳0,在0時區看來是1970-01-01 00:00:00,在東八區看來是1970-01-01 08:00:00。
hive常用時間操作示例
-- 返回UNIX時間戳代表的(格林威治零時區)時間,默認格式如下。
select from_unixtime(1);
1970-01-01 00:00:01
select from_unixtime(1 ,'yyyyMMdd hh:mm:ss');
19700101 12:00:01
-- 獲取當前時間的UNIX時間戳(時區無關的),返回值bigint(對應spark中Long)。
select unix_timestamp();
1579508709
-- 獲取日期部分
select to_date('2020-01-20 11:40:03');
2020-01-20
-- 同樣還有返回年、月、日、時、分、秒、周的函數
select year('2020-01-20 11:40:03');
2020
select year('2020-01-20');
2020
-- 獲取月份數
select month('2020-01-20 11:40:03');
1
select month('2020-01-20');
1
-- 獲取月份中的天數
select day('2020-01-20 10:03:01');
20
select day('2020-01-20');
20
select hour('2020-01-20 11:40:01');
11
select hour('11:40:01');
11
-- 獲取時間中的分鐘數
select minute('2020-01-20 11:40:01');
40
-- 獲取時間中的秒數
select second('2020-01-20 11:40:01');
1
-- 獲取當天在一年中的週數
select weekofyear('2020-01-20 11:40:01');
31
--返回兩個日期相隔天數
select datediff('2020-01-20','2019-12-09');
42
--增加天數
select date_add('2020-01-20',10);
2020-01-30
--減少天數
select date_sub('2020-01-20',10);
2020-01-10
-- 1、hive取得當前日期時間:
-- 1.1) 取得當前日期:
select current_date();
2020-01-20
-- 1.2) 取得當前日期時間:
select current_timestamp();
2020-01-20 08:37:45.076
-- 1.3) hive取得當前時間戳:
select unix_timestamp();
1579509477
-- 1.4) 時間戳轉日期:
select from_unixtime(1579509477,'yyyy-MM-dd HH:dd:ss');
2020-01-20 08:20:57
-- 1.5) hive取得當前時間(0時區):
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');
2020-01-20 08:20:00
-- 2、hive自動計算其他日期(昨天,今天):
-- hive中日期加減函數:date_add(start_date,num_days)
-- 2.1) 取得昨天日期:
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1); select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1); select date_format(date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1),'yyyy-MM-dd');
2020-01-19
-- 2.2) 取得明天日期:
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1); select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);
2020-01-21
-- 2.3)hive取得兩個日期之間差值(差值爲天數):
-- datediff(date1,date2):date1大於date2,返回值爲正,否則,返回值爲負。
select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-10));
10
select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),10));
-10
-- 2.4) 字符串轉時間(字符串必須爲:yyyy-MM-dd格式)
select to_date('2020-01-20 12:12:12');
2020-01-20
-- 2.5) 日期、時間戳、字符串類型格式化輸出標準時間格式:
select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');
2020-01-20 09:00:13
select date_format(current_date(),'yyyyMMdd');
20200120
select date_format('2020-01-20','yyyy-MM-dd HH:mm:ss'); --字符串必須滿足yyyy-MM-dd格式
2020-01-20 00:00:00
-- 2.6) utc時間轉換:
select from_utc_timestamp(current_timestamp(),8);
2020-01-20 09:00:51.749
select to_utc_timestamp(current_timestamp(),8);
2020-01-20 09:01:06.89
備註:
作者:Jason Zeng
博客: https://blog.csdn.net/Z645817
GItHub:https://github.com/lovelifeming
嚴正聲明:
1.由於本博客部分資源來自互聯網,版權均歸原作者所有。轉載的目的是用於學術交流與討論學習,將不對任何資源負法律責任。
2.若無意中侵犯到您的版權利益,請來信聯繫我,我會在收到信息後會儘快給予處理!
3.所有資源內容僅供學習交流之用,請勿用作商業用途,謝謝。
4.如有轉發請註明出處,來源於 https://blog.csdn.net/Z645817,謝謝合作。