hive 日期函数

hive outline

链接

hive 日期函数

  • 格式化时间函数:date_format

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

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

在这里插入图片描述

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

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

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

在这里插入图片描述

  • 获取当前日期: current_date

select current_date();
  • 获取当前时间戳: current_timestamp

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

select current_timestamp();
  • 2011-12-07 13:01:03 转UNIX时间戳 1323262863 函数: unix_timestamp

select unix_timestamp("2011-12-07 13:01:03");
-- 输出 1323262863
-- 可以不传参。默认获取当前系统时间
select unix_timestamp();
-- 输出 1705414709
  • UNIX时间戳 1618238391 转日期 2021-04-12 14:39:51 函数: from_unixtime(不指定时区)

注意:

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

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

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

-- 一般常结合 unix_timestamp 使用,即获取当前系统时间
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') as load_time
-- 输出 2024-01-16 14:23:29
  • UNIX时间戳转日期函数: from_utc_timestamp(指定时区)

UNIX时间戳,是以GMT/UTC时刻【即1970-01-01 00:00:00】为基准,到当前时刻所经过的秒数

1)先获取当前系统的UNIX时间戳
select unix_timestamp(); -- 输出 1705415823
2)将它转换为东八区时间,即中国时间(也是北京时间)
select date_format(from_utc_timestamp(1705415823000,'PRC'),'yyyy-MM-dd HH:mm:ss');
select date_format(from_utc_timestamp(1705415823000,'UTC+8'),'yyyy-MM-dd HH:mm:ss');
select date_format(from_utc_timestamp(1705415823000,'GMT+8'),'yyyy-MM-dd HH:mm:ss');
-- 输出 2024-01-16 14:37:03 (3者结果一样)
3)为什么1705415823要乘以1000
1705415823 如果为整数的话,需要乘以1000,不用纠结为什么,这是规定

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

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

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

PRC中国标准时间【“PRC” 是 “People’s Republic of China” 的缩写,代表的意思是中华人民共和国】

  • 指定格式日期转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

  • 2个完整格式的时间相减,获取秒

hive中没有直接让2个完整格式时间相减的函数,而mysql有,即timestampdiff

也可以不去指定yyyy-MM-dd HH:mm:ss

select unix_timestamp('2018-01-01 08:01:36', 'yyyy-MM-dd HH:mm:ss') - unix_timestamp('2018-01-01 08:00:00', 'yyyy-MM-dd HH:mm:ss')
# 结果 96秒

2个完整格式的时间相减,获取分钟

select (unix_timestamp('2018-01-01 08:01:36', 'yyyy-MM-dd HH:mm:ss') - unix_timestamp('2018-01-01 08:00:00', 'yyyy-MM-dd HH:mm:ss'))/60

结果为1.6,,想要为1,如下做法:

select cast((unix_timestamp('2018-01-01 08:01:36', 'yyyy-MM-dd HH:mm:ss') - unix_timestamp('2018-01-01 08:00:00', 'yyyy-MM-dd HH:mm:ss'))/60 as int)
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Hive提供了一些日期函数,可以方便地对日期进行计算和操作。要获取昨天的日期,可以使用Hive内置的日期函数来实现。可以使用date_sub函数来减去指定的天数,从而获取昨天的日期。具体的使用方法如下所示: ```sql SELECT date_sub(current_date(), 1) AS yesterday; ``` 这个查询会返回昨天的日期。其中,current_date()函数用于获取当前日期,date_sub函数将当前日期减去1天,得到昨天的日期。运行以上查询后,你将得到昨天的日期作为查询结果。 此外,你也可以使用date_add函数来实现相同的效果。使用date_add函数时,你需要指定负数作为第二个参数,表示减去指定的天数。以下是使用date_add函数的示例查询: ```sql SELECT date_add(current_date(), -1) AS yesterday; ``` 这个查询也会返回昨天的日期。在这个示例中,current_date()函数用于获取当前日期,date_add函数将当前日期减去1天,得到昨天的日期。运行以上查询后,你将得到昨天的日期作为查询结果。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [hive日期函数](https://blog.csdn.net/moose_killer/article/details/124389184)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Hive自定义函数](https://download.csdn.net/download/weixin_38733333/14885908)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值