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