一. 日期>>>>时间戳
- unix_timestamp(string date,string pattern) 将指定时间字符串格式字符串转化成unix时间戳,如不符合则返回null
例如:
select unix_timestamp('2019-08-15','yyyy-MM-dd') --1565798400
select unix_timestamp('2019-08-15 16:40:00','yyyy-MM-dd HH:mm:ss') --1565858400
select unix_timestamp('2019-08-15','yyyy-MM-dd HH:mm:ss') --null
二. 时间戳>>>>日期
- from_unixtime(bigint unixtime,string format) 将时间戳秒数转化为UTC时间,并用字符串表示,可通过format规定的时间格式
例如:
select from_unixtime(1565858389,'yyyy-MM-dd HH:mm:ss') --2019-08-15 16:39:49
select from_unixtime(1565858389,'yyyy-MM-dd') --2019-08-15
获取到当前时间:
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') -- 2019-10-15 16:20:50
联合使用:可日期格式的转换
--20191019转成2019-10-19
select from_unixtime(unix_timestamp('20191019','yyyymmdd'),'yyyy-mm-dd') from test;
--2019-10-19 转成20191019
select from_unixtime(unix_timestamp('2019-10-19','yyyy-mm-dd'),'yyyymmdd') from test;
获取当前日期:
--1. 获取当前日期和时间(年月日时分秒)
--写法一:
select from_unixtime(unix_timestamp(),"yyyy-MM-dd HH:mm:ss")
2020-04-21 11:02:55
--写法二:
select substr(current_timestamp(),1,19)
2020-04-21 11:02:55
-- 2.获取当前日期
--写法一:
select from_unixtime(unix_timestamp(),"yyyy-MM-dd")
2020-04-21
--写法二:(推荐)
select current_date()或者select current_date
2020-04-21
-- 写法三:
select substr(current_timestamp(),1,10)
2020-04-21
码字不易,喜欢请点赞,谢谢!!!😊