oracle->mm/MM代表月份,mi代表分鍾,其它大小寫沒有關系
hive->YYYY/yyyy-MM-dd HH:mm:ss 大MM代表月份,小mm代表分外皮,大HH為24小時制,小hh為12小時制,只有大 YYYY和小yyyy不區分
hive中字符串的格式和后面的要求的格式必須保持一致,見下如下紅色標注的,不然返回NULL,
固定日期轉換成時間戳 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
hive中的to_date和to_char
hive的日期格式可由String類型保存,只能識別兩種格式yyyy-MM-dd和yyyy-MM-dd HH:mm:ss。
只要將日期轉為這兩種格式hive就能識別為日期。也就是不管to_date、to_char都是將日期格式化為字符串。
unix_timestamp(日期字符串,日期格式) 返回日期時間戳
from_unixtime(日期時間戳,日期格式) 返回日期字符串
to_date,to_char都用的一個公式,唯一不同的是to_date的目標日期格式是寫死的
1.to_date
from_unixtime(unix_timestamp(來源日期,來源日期格式),'yyyy-MM-dd HH:mm:ss')
例:
from_unixtime(unix_timestamp('2020/02/03 17:35:00','yyyy/MM/dd HH-mm-ss'),'yyyy-MM-dd HH:mm:ss')
2.to_char
from_unixtime(unix_timestamp(來源日期,來源日期格式),目標日期格式)
例:
from_unixtime(unix_timestamp('2020/02/03 17:35:00','yyyy/MM/dd HH-mm-ss'),'yyyy-MM-dd HH:mm:ss')