Hive 中时间,日期函数操作等总结

Hive 中时间,日期函数操作等的总结:

1 获取当前时间 unix_timestamp()
返回值: bigint
说明: 获得当前时区的UNIX时间戳
例子:

hive (dwa)> select unix_timestamp();
OK
1608799047

2 格式化时间 from_unixtime(timestamp,‘formatString’)
返回值: string
说明: 把UNIX时间戳转换成你想要的格式 , 默认是 yyyy-MM-dd HH:mm:ss
例子1 :

hive (dwa)> select from_unixtime(unix_timestamp());
OK
2020-12-24 16:35:59

例子2 :

hive (dwa)> select from_unixtime(unix_timestamp(),'YYYY-MM-dd');
OK
2020-12-24

坑: 注意大小写,注意大小写,注意大小写。 具体看
–> https://www.cnblogs.com/ychang/p/10688833.html

3 时间转时间戳 unix_timestamp(‘formatString’)
返回值: bigint
说明: 把格式化好的时间转换为时间戳(2的相反操作),转换失败返回0

hive (dwa)> select unix_timestamp('20201222 16:44:23','yyyyMMdd HH:mm:ss');
OK
1608626663

4 取部分日期函数
year (string date),

month (string date),
day (string date),
hour (string date),
minute(string date),
second(string date)

返回值:int
说明:取部分日期
例子:

hive (dwa)> select day('2020-12-24 05:01:50');
OK
24

坑:1 失败返回NULL 2 参数不是时间戳

5 计算日期差值函数 datediff(string lastdate, string firstdate)
返回值 int
说明:用第一个日期减去第二个日期,返回相差的天数。 失败返回NULL
例子:

hive (dwa)> select datediff('2020-12-20 23:46:12 ','2020-12-15 1:46:12');
OK
5

坑:1 只是计算到日,不精确到小时。 也就是 2020-12-15 - 2020-12-20
坑:2 不做日期校验, 其实你输入日期不合理也暴力计算 比如 2020-99-99 也是正确的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值