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')