目录

  • ​​hive outline​​
  • ​​hive 日期函数​​
  • ​​格式化时间函数:date_format​​
  • ​​获取当前日期: current_date​​
  • ​​获取当前时间戳: current_timestamp​​
  • ​​获取当前UNIX时间戳函数: unix_timestamp​​
  • ​​UNIX时间戳转日期函数: from_unixtime(不指定时区)​​
  • ​​UNIX时间戳转换为日期函数: from_utc_timestamp(指定时区)​​
  • ​​日期转UNIX时间戳函数: unix_timestamp​​
  • ​​指定格式日期转UNIX时间戳函数: unix_timestamp​​
  • ​​抽取日期函数: to_date​​
  • ​​抽取日期:year month day hour minute second​​
  • ​​日期转周函数: weekofyear​​
  • ​​两日期相减函数: datediff​​
  • ​​日期增加函数: date_add​​
  • ​​日期减少函数: date_sub​​

hive outline

​​链接​​

hive 日期函数

格式化时间函数:date_format

​格式化时间的前提是 xxxx-xx-xx,以-分割​

select date_format('2019-06-29','MM-dd');

hive 日期函数_UNIX

​处理xxxx/xx/xx格式的日期​

​把日期格式由2019/06/29替换为2019-06-29​

select regexp_replace('2019/06/29','/','-');

hive 日期函数_hive_02

获取当前日期: current_date

select current_date();

获取当前时间戳: current_timestamp

同一查询中对current_timestamp的所有调用均返回相同的值

select current_timestamp();

获取当前UNIX时间戳函数: unix_timestamp

UNIX时间戳,是以的GMT/UTC时间(0时区)的【1970-01-01 T00:00:00】时刻为基准,到当前时刻所经过的秒数,与当前系统所处的时区无关,同一时刻在任何时区获取的时间戳都是一样的

select unix_timestamp();

UNIX时间戳转日期函数: from_unixtime(不指定时区)

UNIX时间戳,是以的GMT/UTC时间(0时区)的【1970-01-01 00:00:00】时刻为基准,到当前时刻所经过的秒数,与当前系统所处的时区无关,同一时刻在任何时区获取的时间戳都是一样的

select from_unixtime(1618238391);
-- 输出 2021-04-12 14:39:51

select from_unixtime(1618238391, 'yyyy-MM-dd');
-- 输出 2021-04-12

UNIX时间戳转换为日期函数: from_utc_timestamp(指定时区)

UNIX时间戳,是以的GMT/UTC时间(0时区)的【1970-01-01 00:00:00】时刻为基准,到当前时刻所经过的秒数,与当前系统所处的时区无关,同一时刻在任何时区获取的时间戳都是一样的

-- 假设有一个时间戳为 1218182888,将它转换为东八区时间,即中国时间(也是北京时间)
select date_format(from_utc_timestamp(1218182888000,'PRC'),'yyyy-MM-dd HH:mm:ss');
select date_format(from_utc_timestamp(1218182888000,'UTC+8'),'yyyy-MM-dd HH:mm:ss');
select date_format(from_utc_timestamp(1218182888000,'GMT+8'),'yyyy-MM-dd HH:mm:ss');
-- 输出 2008-08-08 16:08:08 (3者结果一样)
-- 1218182888如果为整数的话,需要乘以1000,小数的话,不用乘1000。不用纠结为什么,这是规定

​以前​​世界标准时间采用的是,格林威治标准时间(GMT)

​现在​​世界标准时间采用的是,协调世界时(UTC)

​不管​​​是采用哪个标准下的时间,​​都​​要表示出来中国属于哪个区,中国就属于东八区,+8表示东八区

日期转UNIX时间戳函数: unix_timestamp

select unix_timestamp("2011-12-07 13:01:03");
-- 输出 1323262863

指定格式日期转UNIX时间戳函数: unix_timestamp

select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss');
-- 输出 1323262863

抽取日期函数: to_date

select to_date('2009-07-30 04:17:52');
-- 输出 2009-07-30

抽取日期:year month day hour minute second

select year('2009-07-30 04:17:52');
-- 输出 2009

日期转周函数: weekofyear

-- weekofyear 返回指定日期所示年份第几周
select weekofyear('2009-07-30 04:17:52');
-- 输出 31

两日期相减函数: datediff

​语法:​​日期格式要求’yyyy-MM-dd HH:mm:ss’ or ‘yyyy-MM-dd’

select  datediff('2012-12-08','2012-05-09');
-- 输出 213

日期增加函数: date_add

select date_add('2012-02-18',10);
-- 输出 2012-02-28

日期减少函数: date_sub