获取当前时间

  • 获取当前时间戳
select unix_timestamp()
  • 把时间戳转为正常的日期
select from_unixtime(unix_timestamp(),'yyyy-mm-dd hh:mm:ss')
select from_unixtime(unix_timestamp(),'yyyy-mm-dd') 
  • 业务中有时存放的是包含毫秒的整数,需要先转换为秒
select from_unixtime(cast(create_time/1000 as bigint),'yyyymmdd') as dt
  • 返回当天三种方式
select current_date;   --2017-06-15
select current_date();   -- 2021-10-22

select current_timestamp; --返回时分秒
--2018-06-18 10:37:53.278
select from_unixtime(unix_timestamp());
--2017-06-15 19:55:04

日期格式转换

  • 日期格式转换 yyyymmdd—>yyyy-mm-dd
select from_unixtime(unix_timestamp('20211022','yyyymmdd'),"yyyy-mm-dd");
2021-10-22
  • 固定日期转换成时间戳
select unix_timestamp('2016-08-16','yyyy-mm-dd') --1471276800
select unix_timestamp('20160816','yyyymmdd') --1471276800
select unix_timestamp('2016-08-16t10:02:41z', "yyyy-mm-dd't'hh:mm:ss'z'") --1471312961
16/mar/2017:12:25:01 +0800 转成正常格式(yyyy-mm-dd hh:mm:ss)
select from_unixtime(to_unix_timestamp('16/mar/2017:12:25:01 +0800', 'dd/mmm/yyy:hh:mm:ss z'))
  • 时间戳转换程固定日期
select from_unixtime(1471276800,'yyyy-mm-dd') --2016-08-16
select from_unixtime(1471276800,'yyyymmdd') --20160816
select from_unixtime(1471312961) --    2016-08-16 10:02:41
select from_unixtime( unix_timestamp('20160816','yyyymmdd'),'yyyy-mm-dd')  --2016-08-16
select date_format('2016-08-16','yyyymmdd') --20160816
  • 字符串强制转换,获取日期
select to_date('2016-08-16 10:03:01') --2016-08-16
类似sql 中的date
  • 截取日期部分
select substr('2021-10-22 17:34:56',1,10)
2021-10-22

select date_format('2021-10-22 17:34:56','yyyy-mm-dd')
2021-10-22

返回日期中的年,月,日,时,分,秒,当前的周数

  • 返回日期中的年
select year('2016-08-16 10:03:01') --2016
  • 返回日期中的月
select month('2016-08-16 10:03:01') --8
  • 返回日期中的日
select day('2016-08-16 10:03:01') --16
  • 返回日期中的时
select hour('2016-08-16 10:03:01') --10
  • 返回日期中的分
select minute('2016-08-16 10:03:01') --3
  • 返回日期中的秒
select second('2016-08-16 10:03:01') --1
  • 返回日期在当前的周数
select weekofyear('2016-08-16 10:03:01') --33

计算日期差值

  • 返回结束日期减去开始日期的天数
select datediff('2016-08-16','2016-08-11') 
  • 返回开始日期startdate增加days天后的日期
select date_add('2016-08-16',10)
  • 返回开始日期startdate减少days天后的日期
select date_sub('2016-08-16',10)

前一日/昨日
select date_sub(current_date(),1);
2021-10-21

最近一个月/30天
select date_sub(current_date(),30);
2021-09-22
  • 前一日12点/昨日12点
select concat(date_format(date_sub(current_date(),1),'yyyy-mm-dd'),' ','12');
2021-10-21 12

返回当月或当年的第一天

  • 返回当月的第一天
select trunc('2016-08-16','mm') --2016-08-01

select date_format(to_date(trunc(current_date(),'mm')),"yyyy-mm-dd");
2021-10-01
  • 返回当年的第一天
select trunc('2016-08-16','year') --2016-01-01

参考汇总

固定日期转换成时间戳
select unix_timestamp('2016-08-16','yyyy-mm-dd') --1471276800
select unix_timestamp('20160816','yyyymmdd') --1471276800
select unix_timestamp('2016-08-16t10:02:41z', "yyyy-mm-dd't'hh:mm:ss'z'") --1471312961
16/mar/2017:12:25:01 +0800 转成正常格式(yyyy-mm-dd hh:mm:ss)
select from_unixtime(to_unix_timestamp('16/mar/2017:12:25:01 +0800', 'dd/mmm/yyy:hh:mm:ss z'))
时间戳转换程固定日期
select from_unixtime(1471276800,'yyyy-mm-dd') --2016-08-16
select from_unixtime(1471276800,'yyyymmdd') --20160816
select from_unixtime(1471312961) --    2016-08-16 10:02:41
select from_unixtime( unix_timestamp('20160816','yyyymmdd'),'yyyy-mm-dd')  --2016-08-16
select date_format('2016-08-16','yyyymmdd') --20160816
返回日期时间字段中的日期部分
select to_date('2016-08-16 10:03:01') --2016-08-16
类似sql 中的date
取当前时间
select from_unixtime(unix_timestamp(),'yyyy-mm-dd hh:mm:ss')
select from_unixtime(unix_timestamp(),'yyyy-mm-dd') 
返回日期中的年
select year('2016-08-16 10:03:01') --2016
返回日期中的月
select month('2016-08-16 10:03:01') --8
返回日期中的日
select day('2016-08-16 10:03:01') --16
返回日期中的时
select hour('2016-08-16 10:03:01') --10
返回日期中的分
select minute('2016-08-16 10:03:01') --3
返回日期中的秒
select second('2016-08-16 10:03:01') --1
返回日期在当前的周数
select weekofyear('2016-08-16 10:03:01') --33
返回结束日期减去开始日期的天数
select datediff('2016-08-16','2016-08-11') 
返回开始日期startdate增加days天后的日期
select date_add('2016-08-16',10)
返回开始日期startdate减少days天后的日期
select date_sub('2016-08-16',10)
返回当天三种方式
select current_date;
--2017-06-15
select current_timestamp;--返回时分秒
--2017-06-15 19:54:44
select from_unixtime(unix_timestamp());
--2017-06-15 19:55:04
返回当前时间戳
select current_timestamp--2018-06-18 10:37:53.278
返回当月的第一天
select trunc('2016-08-16','mm') --2016-08-01
返回当年的第一天
select trunc('2016-08-16','year') --2016-01-01

 参考链接:

hive日期格式转换方法总结

以上就是hive常用日期格式转换语法的详细内容,更多关于hive日期格式转换的资料请关注其它相关文章!