• hive 时间相关的函数


    yyyy-MM-dd与yyyyMMdd000000转换的三种方法
     
    方法一:date_format(只支持yyyy-MM-dd -> yyyyMMdd000000)
    
    select date_format('2019-10-07', 'yyyyMMdd000000')
    -- 20191007000000
     
    
    方法二:from_unixtime + unix_timestamp
    
    select from_unixtime(unix_timestamp('2019-10-07', 'yyyy-MM-dd'), 'yyyyMMdd000000')
    -- 20191007000000
    
    select from_unixtime(unix_timestamp(substr('20191007000000',1,8),'yyyyMMdd'),'yyyy-MM-dd')
    -- 2019-10-07
    
    
    固定日期转换成时间戳
    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
    
    返回日期时间字段中的日期部分
    select to_date('2016-08-16 10:03:01') --2016-08-16
    取当前时间
    select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')
    select from_unixtime(unix_timestamp(),'yyyy-MM-dd') 
    返回日期中的年
    select year('2016-08-16 10:03:01') --2016
    返回日期中的月
    select month('2016-08-16 10:03:01') --8
    返回日期中的日
    select day('2016-08-16 10:03:01') --16
    返回日期中的时
    select hour('2016-08-16 10:03:01') --10
    返回日期中的分
    select minute('2016-08-16 10:03:01') --3
    返回日期中的秒
    select second('2016-08-16 10:03:01') --1
    
    返回日期在当前的周数
    select weekofyear('2016-08-16 10:03:01') --33
    
    返回结束日期减去开始日期的天数
    select datediff('2016-08-16','2016-08-11') 
    
    返回开始日期startdate增加days天后的日期
    select date_add('2016-08-16',10)
    
    返回开始日期startdate减少days天后的日期
    select date_sub('2016-08-16',10)
    
    返回当天三种方式
    SELECT CURRENT_DATE;
    --2017-06-15
    SELECT CURRENT_TIMESTAMP;--返回时分秒
    --2017-06-15 19:54:44
    SELECT from_unixtime(unix_timestamp());
    --2017-06-15 19:55:04
    返回当前时间戳
    Select current_timestamp--2018-06-18 10:37:53.278
    
    返回当月的第一天
    select trunc('2016-08-16','MM') --2016-08-01
    返回当年的第一天
    select trunc('2016-08-16','YEAR') --2016-01-01
    

    文章转自:https://www.cnblogs.com/littlewu/p/9364273.html

  • 相关阅读:
    【JS】修改字体
    【git】.gitignore文件常用设置
    【CSS】让textarea在div里水平垂直都居中的三种办法
    【JS】随着进度条进展,逐个显示li节点
    【MySQL】The server time zone value 'xxxxxx' is unrecognized or represents more than one time zone 解决方案
    浅谈Web前端开发中的Touch事件
    [转]PostgreSQL与MySQL比较
    RDLC:An error occurred during local report processing
    [转]让你的PHP更安全
    [转]Win7或Windows server 2008中IIS7支持ASP+Access解决方法
  • 原文地址:https://www.cnblogs.com/zhqin/p/13524636.html
Copyright © 2020-2023  润新知