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 也是正确的