Apache Hive: How to convert string to timestamp?

有些话、适合烂在心里 提交于 2019-11-30 21:45:25

Try this :

select from_unixtime(unix_timestamp("Sun Jul 31 09:28:20 UTC 2016","EEE MMM dd HH:mm:ss zzz yyyy"));

This works fine if your hive cluster has UTC timezone. Say suppose your server is in CST then you need to do as below to get to UTC;

select to_utc_timestamp(from_unixtime(unix_timestamp("Sun Jul 31 09:28:20 UTC 2016","EEE MMM dd HH:mm:ss zzz yyyy")),'CST');

Hope this helps.

EDIT Hive date functions use the JAVA simple date formater for the patterns . Refer this for the patterns.

Be aware my computers runs on PDT

[cloudera@quickstart ~]$ date +%Z
PDT

So the UTC time is converted to 2:28:20 PDT. Anyway this is not the point. You are using HH for hours, use hh and you need at least 3 M for the month.

0: jdbc:hive2://quickstart:10000/default> select from_unixtime(unix_timestamp("Sun Jul 31 09:28:20 UTC 2016", 'E MMM dd hh:mm:ss z yyyy')) as date;
+----------------------+--+
|         date         |
+----------------------+--+
| 2016-07-31 02:28:20  |
+----------------------+--+
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!