目录获取当前时间日期格式转换返回日期中的年,月,日,时,分,秒,当前的周数计算日期差值返回当月或当年的第一天参考汇总
获取当前时间获取当前时间戳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-15select current_date(); — 2021-10-22SELECT current_timestamp; –返回时分秒–2018-06-18 10:37:53.278SELECT from_unixtime(unix_timestamp());–2017-06-15 19:55:04
日期格式转换日期格式转换 yyyyMMdd—>yyyy-MM-ddselect from_unixtime(unix_timestamp(\’20211022\’,\’yyyyMMdd\’),\”yyyy-MM-dd\”);2021-10-22固定日期转换成时间戳select unix_timestamp(\’2016-08-16\’,\’yyyy-MM-dd\’) –1471276800select unix_timestamp(\’20160816\’,\’yyyyMMdd\’) –1471276800select unix_timestamp(\’2016-08-16T10:02:41Z\’, \”yyyy-MM-dd\’T\’HH:mm:ss\’Z\’\”) –147131296116/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-16select from_unixtime(1471276800,\’yyyyMMdd\’) –20160816select from_unixtime(1471312961) — 2016-08-16 10:02:41select from_unixtime( unix_timestamp(\’20160816\’,\’yyyyMMdd\’),\’yyyy-MM-dd\’) –2016-08-16select 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-22select 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-01select 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\’) –1471276800select unix_timestamp(\’20160816\’,\’yyyyMMdd\’) –1471276800select unix_timestamp(\’2016-08-16T10:02:41Z\’, \”yyyy-MM-dd\’T\’HH:mm:ss\’Z\’\”) –147131296116/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-16select from_unixtime(1471276800,\’yyyyMMdd\’) –20160816select from_unixtime(1471312961) — 2016-08-16 10:02:41select from_unixtime( unix_timestamp(\’20160816\’,\’yyyyMMdd\’),\’yyyy-MM-dd\’) –2016-08-16select 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-15SELECT CURRENT_TIMESTAMP;–返回时分秒–2017-06-15 19:54:44SELECT 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日期格式转换的资料请关注悠久资源其它相关文章!