如何转换日期
How to convert date
我在 table 中的日期是 2017 年 9 月 1 日 2:00 下午作为实际发货日期 我想在配置单元中将其转换为 01-09-2017 我尝试使用以下命令但显示为空 select actualshipdate,from_unixtime(unix_timestamp(substr(actualshipdate,0,11), 'dd-mm-yyyy')) as newdate from tablename;
使用 unix_timestamp(string date, string pattern)
将给定日期格式转换为自 1970-01-01 以来的秒数。然后使用 from_unixtime()
转换为给定格式:
hive> select from_unixtime(unix_timestamp('Sep 1 2017 2:00 PM' ,'MMM dd yyyy HH:mm a'), 'dd-MM-yyyy');
OK
01-09-2017
Time taken: 0.049 seconds, Fetched: 1 row(s)
在此处查看模式示例:https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
您使用的函数正确,但参数错误。参数应该是 unix_timestamp(datestring, date_format) ;此函数会将日期转换为 unix 日期格式,您可以使用 from_unixtime(unixdateformat,format_youneed);
进一步格式化
hive> select unix_timestamp('Sep 1 2017 2:00 PM' ,'MMM dd yyyy HH:mm a');
OK
1504256400
您需要特定的日期模式,您可以使用函数
from_unixtime(unixdateformat,format_youneed);
hive> select from_unixtime(1504256400,'dd-MM-yyyy');
OK
01-09-2017
hive> select from_unixtime(UNIX_TIMESTAMP('Sep 1 2017 2:00 PM','MMM dd yyyy
HH:mm a'), 'dd-MM-yyyy');
OK
01-09-2017
Time taken: 0.135 seconds, Fetched: 1 row(s)
由于您在 table 中的实际日期中存储了日期,因此您可以使用以下命令获取结果。
**hive> select from_unixtime(UNIX_TIMESTAMP(actualshipdate,'MMM dd yyyy HH:mm a'), 'dd-MM-yyyy') from tablename;**
我在 table 中的日期是 2017 年 9 月 1 日 2:00 下午作为实际发货日期 我想在配置单元中将其转换为 01-09-2017 我尝试使用以下命令但显示为空 select actualshipdate,from_unixtime(unix_timestamp(substr(actualshipdate,0,11), 'dd-mm-yyyy')) as newdate from tablename;
使用 unix_timestamp(string date, string pattern)
将给定日期格式转换为自 1970-01-01 以来的秒数。然后使用 from_unixtime()
转换为给定格式:
hive> select from_unixtime(unix_timestamp('Sep 1 2017 2:00 PM' ,'MMM dd yyyy HH:mm a'), 'dd-MM-yyyy');
OK
01-09-2017
Time taken: 0.049 seconds, Fetched: 1 row(s)
在此处查看模式示例:https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
您使用的函数正确,但参数错误。参数应该是 unix_timestamp(datestring, date_format) ;此函数会将日期转换为 unix 日期格式,您可以使用 from_unixtime(unixdateformat,format_youneed);
进一步格式化hive> select unix_timestamp('Sep 1 2017 2:00 PM' ,'MMM dd yyyy HH:mm a');
OK
1504256400
您需要特定的日期模式,您可以使用函数
from_unixtime(unixdateformat,format_youneed);
hive> select from_unixtime(1504256400,'dd-MM-yyyy');
OK
01-09-2017
hive> select from_unixtime(UNIX_TIMESTAMP('Sep 1 2017 2:00 PM','MMM dd yyyy
HH:mm a'), 'dd-MM-yyyy');
OK
01-09-2017
Time taken: 0.135 seconds, Fetched: 1 row(s)
由于您在 table 中的实际日期中存储了日期,因此您可以使用以下命令获取结果。
**hive> select from_unixtime(UNIX_TIMESTAMP(actualshipdate,'MMM dd yyyy HH:mm a'), 'dd-MM-yyyy') from tablename;**