hive时间函数

 1.unix_timestamp()

返回当前时区的unix时间戳
返回类型:bigint
hive (tmp)> select unix_timestamp() 
1465875016

2.from_unixtime(bigint unixtime[,string format])

时间戳转日期函数
返回类型:string
hive (tmp)> select from_unixtime(unix_timestamp(),'yyyy-MM-dd') ;

2022-12-28

3.unix_timestamp(string date)

返回指定日期格式的的时间戳
返回类型:bigint
注意:如果后面只有date参数,date的形式必须为’yyyy-MM-dd HH:mm:ss’的形式。
hive (tmp)> select unix_timestamp('2022-12-28') ;
NULL
hive (tmp)> select unix_timestamp('2022-12-28 00:00:00') ;

1672185600

4.unix_timestamp(string date,string pattern)

返回指定日期格式的时间戳
返回类型:bigint
hive (tmp)> select unix_timestamp('2022-12-28','yyyy-MM-dd') ;
1672185600

5.to_date(string date)

返回时间字段中的日期部分
返回类型:string
hive (tmp)> select to_date('2022-12-28 00:00:00') ;

2022-12-28

6.year(string date)

返回时间字段中的年
返回类型:int
hive (tmp)> select year('2022-12-28 00:00:00') ;
2022

7.month(string date)

返回时间字段中的月
返回类型:int
hive (tmp)> select month('2022-12-28');
12

8.day(string date)

返回时间字段中的天
返回类型:int
hive (tmp)> select day('2022-12-28') ;
28

9.weekofyear(string date)

返回时间字段是本年的第多少周
返回类型:int
hive (tmp)> select weekofyear('2022-12-28') ;
52

10.datediff(string enddate,string begindate)


返回enddate与begindate之间的时间差的天数
返回类型:int
hive (tmp)> select datediff('2022-12-29','2022-12-28') ;
1

spark-sql> select DATEDIFF(from_utc_timestamp(FROM_UNIXTIME(1565750800000 / 1000),'GMT+1') ,from_utc_timestamp(FROM_UNIXTIME(1565683911340 / 1000),'GMT+1') );
1

11.date_add(string date,int days)

返回date增加days天后的日期
返回类型:string
hive (tmp)> select date_add('2022-12-28',15) ;

2023-01-12

12.date_sub(string date,int days)

返回date减少days天后的日期
返回类型:string
hive (tmp)> select date_sub('2022-12-28',15) ;
2022-12-13

13.CURRENT_TIMESTAMP

获取当前日期 UTC 默认时区

返回类型:string

spark-sql> select CURRENT_TIMESTAMP();
2022-12-28 10:21:10.214

14.时区时间获取

时间戳是UTC并转换为给定的时区UTC 默认时区 转为 GMT+1 时区

spark-sql> SELECT from_utc_timestamp(CURRENT_TIMESTAMP,'GMT+1') ;
2022-12-28 11:25:24.467

15.时间戳转日期

时间戳是GMT+1,转GMT+1日期

select from_utc_timestamp(FROM_UNIXTIME(1565750800000 / 1000),'GMT+1');


from_utc_timestamp(CAST(from_unixtime(CAST((CAST(1565750800000 AS DOUBLE) / CAST(1000 AS DOUBLE)) AS BIGINT), yyyy-MM-dd HH:mm:ss) AS TIMESTAMP), GMT+1)
2019-08-14 03:46:40

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值