• Hive数据库日期格式化


    1,获取当前日期

    select current_date() from table;
    返回 :‘2022-06-07’

    2,获取当前时间

    select current_timestamp() from table;
    返回:‘2022-06-07 13:20:00.00’

    3,获取当前时间戳

    返回类型为int

    (1)转换当前的日期为时间戳

    select unix_timestamp(current_date()) from table;

    (2)转换当前的时间为时间戳

    select unix_timestamp(current_timestamp()) from table;

    4,时间转换为固定格式类型

    函数:from_unixtime()
    格式:select from_unixtime(时间戳,时间格式)
    示例:
    select from_unixtime(1654580056) from table;
    返回:‘2022-06-07 13:34:16’

    select from_unixtime(1654580056,‘YYYY-MM-dd’) from table;
    返回:‘2022-06-07’

    select from_unixtime(1654580056,‘YYYYMMdd’) from table;
    返回:‘20220607’

    select data_format(‘2022-06-07’,‘yyyyMMdd’) from table;
    返回:‘20220607’

    5,返回日期时间字段中的部分日期

    (1)返回日期中的年

    select year(‘2022-06-07 13:34:16’) --2022

    (2)返回日期中的月

    select month(‘2022-06-07 13:34:16’) --06

    (3)返回日期中的日

    select day(‘2022-06-07 13:34:16’) --07

    (4)返回日期中的时

    select hour(‘2022-06-07 13:34:16’) --13

    (5)返回日期中的分

    select minute(‘2022-06-07 13:34:16’) --34

    (6)返回日期中的秒

    select second(‘2022-06-07 13:34:16’) --16

    (7)返回日期中的年月日

    select to_date(‘2022-06-07 13:34:16’,‘yyyyMMdd’) from table;

    6,返回当月第一天

    select trunc(‘2022-06-07’,‘MM’)
    –2022-06-01

    7,返回当年第一天

    select trunc(‘2022-06-07’,‘YEAR’)
    –2022-01-01

    8,两个日期计算时间差

    函数:datediff(开始日期,结束日期)
    select datediff(‘2022-06-07’,‘2022-05-11’)
    返回:27

    9,开始日期增加N天后的日期

    函数:date_add(开始日期,天数)
    select date_add(‘2022-06-07’,10) from table;
    返回:2022-06-17

    10,开始日期减少N天后的日期

    函数:date_sub(开始日期,天数)
    select date_sub(‘2022-06-07’,5) from table;
    返回:2022-06-02

  • 相关阅读:
    本地启动RocketMQ未映射主机名产生的超时问题
    金九银十,如何面试拿下大厂offer?(附大厂面经+面试宝典)
    关于我对axios的源码理解
    6、Powershell命令配置Citrix PVS云桌面桌面注销不关机
    C4D遇到的动力学模拟问题怎么办?看完本文就知道
    CentOS7安装superset2.0
    34.数据库MySQL(1)
    C++的IO流
    深入理解 Python 虚拟机:元组(tuple)的实现原理及源码剖析
    小皮面板为什么还是打不开?
  • 原文地址:https://blog.csdn.net/weixin_42011858/article/details/125612676