【问题标题】:How to convert date如何转换日期
【发布时间】:2018-02-16 23:07:30
【问题描述】:

我在表格中有日期为 2017 年 9 月 1 日下午 2:00 作为 actualshipdate 我想在 hive 中将其转换为 01-09-2017 我尝试使用以下命令但显示为空 select actualshipdate,from_unixtime(unix_timestamp(substr(actualshipdate ,0,11), 'dd-mm-yyyy')) as newdate from tablename;

【问题讨论】:

标签: hive hiveql


【解决方案1】:

使用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

【讨论】:

    【解决方案2】:

    您使用了正确的功能,但参数错误。参数应该是 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)
    

    由于您将日期存储在表中的实际日期中,您可以使用以下命令来获取结果。

    **hive>  select from_unixtime(UNIX_TIMESTAMP(actualshipdate,'MMM dd yyyy HH:mm a'), 'dd-MM-yyyy') from tablename;**
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 2017-04-15
      • 2011-04-14
      • 2010-11-03
      • 2020-03-19
      • 2012-08-12
      • 2019-06-14
      相关资源
      最近更新 更多