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');
相关推荐
G皮T14 分钟前
【Elasticsearch】检索排序 & 分页
大数据·elasticsearch·搜索引擎·排序·分页·检索·深度分页
无级程序员2 小时前
hive2服务启动报错:/tmp/hive on HDFS should be writable(不是chmod 777能解决的)
hive·hadoop·hdfs
羊小猪~~3 小时前
数据库学习笔记(十七)--触发器的使用
数据库·人工智能·后端·sql·深度学习·mysql·考研
小新学习屋4 小时前
Spark从入门到熟悉(篇三)
大数据·分布式·spark
rui锐rui4 小时前
大数据学习2:HIve
大数据·hive·学习
G皮T4 小时前
【Elasticsearch】检索高亮
大数据·elasticsearch·搜索引擎·全文检索·kibana·检索·高亮
凌辰揽月7 小时前
Servlet学习
hive·学习·servlet
zskj_zhyl9 小时前
智慧养老丨从依赖式养老到自主式养老:如何重构晚年生活新范式
大数据·人工智能·物联网
先睡9 小时前
优化MySQL查询
数据库·sql