Hive 时间操作

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
博客:http://www.cnblogs.com/zengming/ 
GItHub:https://github.com/lovelifeming
严正声明:
1.由于本博客部分资源来自互联网,版权均归原作者所有。转载的目的是用于学术交流与讨论学习,将不对任何资源负法律责任。
2.若无意中侵犯到您的版权利益,请来信联系我,我会在收到信息后会尽快给予处理!
3.所有资源内容仅供学习交流之用,请勿用作商业用途,谢谢。
4.如有转发请注明出处,来源于http://www.cnblogs.com/zengming/,谢谢合作。

原文地址:https://www.cnblogs.com/zengming/p/12218793.html