Hive的基础函数-日期函数

当前系统时间函数:current_date()、current_timestamp()、unix_timestamp()

复制代码
-- 函数1:current_date();		
	当前系统日期		格式:"yyyy-MM-dd"
-- 函数2:current_timestamp();	
	当前系统时间戳:	格式:"yyyy-MM-dd HH:mm:ss.ms"
-- 函数3:unix_timestamp();	
	当前系统时间戳	格式:距离1970年1月1日0点的秒数。
	select unix_timestamp(); //获取当前的时间戳
  select unix_timestamp('2017/1/21','yyyy/MM/dd') ;


hive (yhdb)> select current_date();
OK
_c0
2023-08-26
Time taken: 1.032 seconds, Fetched: 1 row(s)
hive (yhdb)> select current_timestamp();
OK
_c0
2023-08-26 18:38:02.304
Time taken: 0.404 seconds, Fetched: 1 row(s)

日期转时间戳函数:unix_timestamp()

复制代码
获取当前时间的时间戳
select unix_timestamp();
select unix_timestamp(current_timestamp());

hive (yhdb)> select unix_timestamp();
unix_timestamp(void) is deprecated. Use current_timestamp instead.
unix_timestamp(void) is deprecated. Use current_timestamp instead.
OK
_c0
1693046302
Time taken: 0.448 seconds, Fetched: 1 row(s)
hive (yhdb)> select unix_timestamp('2022-10-01 00:00:00');
OK
_c0
1664582400
Time taken: 0.322 seconds, Fetched: 1 row(s)

hive (yhdb)> select unix_timestamp('2022/10/01');
OK
_c0
NULL
Time taken: 0.473 seconds, Fetched: 1 row(s)
hive (yhdb)> select unix_timestamp('2022/10/01','yyyy/MM/dd');
OK
_c0
1664582400
Time taken: 0.336 seconds, Fetched: 1 row(s)

时间戳转日期函数:from_unixtime

复制代码
根据时间戳,指定输出日期的格式:
hive (yhdb)> select from_unixtime(1693046606);
OK
_c0
2023-08-26 10:43:26
Time taken: 0.39 seconds, Fetched: 1 row(s)
hive (yhdb)> select from_unixtime(1693046606,'yyyy/MM/dd HH~mm~ss');
OK
_c0
2023/08/26 10~43~26
Time taken: 1.367 seconds, Fetched: 1 row(s)

总而言之

select from_unixtime(1724722412+8*3600);

from_unixtime 换算的时间出来之后,和真实的时间相差 8 小时。所以要+8 小时的秒值

计算时间差函数:datediff()、months_between()

复制代码
select datediff('2023-01-01','2023-02-01');

hive (yhdb)> select datediff('2023-01-01','2023-02-01');
OK
_c0
-31
出现负数,说明数据是前面的时间减去后面的时间,相差的天数。

hive (yhdb)> select months_between('2019-12-20','2019-11-01');
OK
_c0
1.61290323
Time taken: 0.3 seconds, Fetched: 1 row(s)
前面的时间减去后面时间的月数 ,可以精确到小数。
日期相加:
select date_add('2023-12-31',3); -- 日期相加  2024-01-03
日期相减
select date_sub('2023-12-31',3);
select add_months('2023-12-31',3); --月份相加 2024-03-31

日期时间分量函数:year()、month()、day()、hour()、minute()、second()

复制代码
select month(current_date());

hive (yhdb)> select month(current_date());
OK
_c0
8
Time taken: 0.395 seconds, Fetched: 1 row(s)
hive (yhdb)> select year(current_date());
OK
_c0
2023
Time taken: 0.374 seconds, Fetched: 1 row(s)
hive (yhdb)> select year('2012-12-12');
OK
_c0
2012
Time taken: 0.395 seconds, Fetched: 1 row(s)

日期定位函数:last_day()、next_day()

复制代码
--月末:
select  last_day(current_date());
--可以求出当前日期的下个星期几
select next_day(current_date(),'thursday');

日期加减函数:date_add()、date_sub()、add_months()

复制代码
select date_add(current_date,1);
select date_sub(current_date,90);
select add_months(current_date,1);

综合练习:

复制代码
--当月第1天
当前月往前推一个月,7月,7月的最后一天,+1
select  date_add(last_day(add_months(current_date,-1)),1);

--下个月第1天:
select date_add(last_day(current_date),1);

dayofmonth(日期): 当前时间是这个月的第几天
select  add_months(date_sub(current_date,dayofmonth(current_date)-1),1);

字符串转日期:to_date()

复制代码
select to_date('2023-07-01');

将日期转为字符串:date_format()

复制代码
select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');
select date_format(current_timestamp(),'yyyy/MM/dd');
select date_format('2017-01-01','yyyy-MM-dd HH:mm:ss');

需求:假如需要将'2024-12-31' --> '2024/12/31'
-- 第一种方案
select date_format('2024-12-31','yyyy~MM~dd');
-- from_unixtime  将一个时间戳转换为某种字符串类型
-- 方案二
select from_unixtime(unix_timestamp('2024-12-31','yyyy-MM-dd'),'yyyy/MM/dd');
相关推荐
RestCloud几秒前
ETL中三种数据加载性能优化的方式
数据仓库·性能优化·etl·数字化转型·数据集成平台·数据加载方式
阿里云大数据AI技术17 分钟前
一招教你轻松调用大模型来处理海量数据
大数据·deepseek
RestCloud43 分钟前
ETL 自动化:提升数据处理效率与准确性的核心驱动力
数据仓库·自动化·api·etl·数字化转型·数据集成平台
兮动人1 小时前
【sql优化】where 1=1
数据库·sql·sql优化·where 1=1
一个天蝎座 白勺 程序猿1 小时前
大数据(2)Hadoop架构深度拆解:HDFS与MapReduce企业级实战与高阶调优
大数据·hadoop·架构
北京天拓四方1 小时前
IOT平台助力高端装备制造企业突破发展瓶颈
大数据·人工智能·物联网
软信数据研究院3 小时前
软信天成:从“等数据”到“用数据”,自助数据分析如何让企业快人一步?
大数据·信息可视化·数据挖掘·数据分析·数据治理·数据管理·自助数据分析
春风化作秋雨4 小时前
什么是矩阵账号
大数据·线性代数·矩阵
阿里云大数据AI技术4 小时前
谁是 AI 搜索先锋?阿里云携手 Elastic 正式启动先锋者招募!
大数据·elasticsearch
酷爱码4 小时前
hadoop相关面试题以及答案
大数据·hadoop·分布式