Hive 时间转换

UNIX时间戳概念:因为UNIX时间戳只是一个秒数,一个UNIX时间戳在不同时区看来,时间是不同的。
如UNIX时间戳0,在0时区看来是1970-01-01 00:00:00,在东八区看来是1970-01-01 08:00:00。

hive常用时间操作示例

-- 返回UNIX时间戳代表的(格林威治零时区)时间,默认格式如下。

select from_unixtime(1);

  1970-01-01 00:00:01

select from_unixtime(1 ,'yyyyMMdd hh:mm:ss');

  19700101 12:00:01

-- 获取当前时间的UNIX时间戳(时区无关的),返回值bigint(对应spark中Long)。

select unix_timestamp();

  1579508709

-- 获取日期部分

select to_date('2020-01-20 11:40:03');

  2020-01-20

-- 同样还有返回年、月、日、时、分、秒、周的函数

select year('2020-01-20 11:40:03');

  2020

select year('2020-01-20');

  2020

-- 获取月份数

select month('2020-01-20 11:40:03');

  1

select month('2020-01-20');

  1

-- 获取月份中的天数

select day('2020-01-20 10:03:01');

  20

select day('2020-01-20');

  20

select hour('2020-01-20 11:40:01');

  11

select hour('11:40:01');

  11

-- 获取时间中的分钟数

select minute('2020-01-20 11:40:01');

  40

-- 获取时间中的秒数

select second('2020-01-20 11:40:01');

  1

-- 获取当天在一年中的周数

select weekofyear('2020-01-20 11:40:01');

  31

--返回两个日期相隔天数

select datediff('2020-01-20','2019-12-09');

  42

--增加天数

select date_add('2020-01-20',10);

  2020-01-30

--减少天数

select date_sub('2020-01-20',10);

  2020-01-10

-- 1、hive取得当前日期时间:

  -- 1.1) 取得当前日期:

select current_date();

  2020-01-20

  -- 1.2) 取得当前日期时间:

select current_timestamp();

  2020-01-20 08:37:45.076

  -- 1.3) hive取得当前时间戳:

select unix_timestamp();

  1579509477

  -- 1.4) 时间戳转日期:

select from_unixtime(1579509477,'yyyy-MM-dd HH:dd:ss');

  2020-01-20 08:20:57

  -- 1.5) hive取得当前时间(0时区):

select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');

  2020-01-20 08:20:00

-- 2、hive自动计算其他日期(昨天,今天):

  -- hive中日期加减函数:date_add(start_date,num_days)

  -- 2.1) 取得昨天日期:

select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);
select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
select date_format(date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1),'yyyy-MM-dd');

  2020-01-19

  -- 2.2) 取得明天日期:

select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);

  2020-01-21

  -- 2.3)hive取得两个日期之间差值(差值为天数):
  -- datediff(date1,date2):date1大于date2,返回值为正,否则,返回值为负。

select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-10));

  10

select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),10));

  -10

  -- 2.4) 字符串转时间(字符串必须为:yyyy-MM-dd格式)

select to_date('2020-01-20 12:12:12');

  2020-01-20

  -- 2.5) 日期、时间戳、字符串类型格式化输出标准时间格式:

select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');

  2020-01-20 09:00:13

select date_format(current_date(),'yyyyMMdd');

  20200120

select date_format('2020-01-20','yyyy-MM-dd HH:mm:ss'); --字符串必须满足yyyy-MM-dd格式

  2020-01-20 00:00:00

  -- 2.6) utc时间转换:

select from_utc_timestamp(current_timestamp(),8);

  2020-01-20 09:00:51.749

select to_utc_timestamp(current_timestamp(),8);

  2020-01-20 09:01:06.89

备注:
作者:Jason Zeng
博客:http://www.cnblogs.com/zengming/ 
GItHub:https://github.com/lovelifeming
严正声明:
1.由于本博客部分资源来自互联网,版权均归原作者所有。转载的目的是用于学术交流与讨论学习,将不对任何资源负法律责任。
2.若无意中侵犯到您的版权利益,请来信联系我,我会在收到信息后会尽快给予处理!
3.所有资源内容仅供学习交流之用,请勿用作商业用途,谢谢。
4.如有转发请注明出处,来源于http://www.cnblogs.com/zengming/,谢谢合作。

Hive 时间操作的更多相关文章

  1. Hive时间操作[转]

    时间字段格式化 from_unixtime(unix_timestamp(VisitTime),'yyyy-MM-dd') 日期函数UNIX时间戳转日期函数: from_unixtime语法:   f ...

  2. HIVE 时间操作函数

    转自http://www.oratea.net/?p=944 日期函数UNIX时间戳转日期函数: from_unixtime语法:   from_unixtime(bigint unixtime[,  ...

  3. Hive 时间操作函数(转)

    1.日期函数UNIX时间戳转日期函数: from_unixtime 语法:   from_unixtime(bigint unixtime[, string format]) 返回值: string ...

  4. spark使用Hive表操作

    spark Hive表操作 之前很长一段时间是通过hiveServer操作Hive表的,一旦hiveServer宕掉就无法进行操作. 比如说一个修改表分区的操作 一.使用HiveServer的方式 v ...

  5. Java Calendar 类的时间操作

    Java Calendar 类的时间操作 标签: javaCalendar时间Date 2013-07-30 17:53 140401人阅读 评论(7) 收藏 举报 分类: 所有(165) Java ...

  6. paip.日期时间操作以及时间戳uapi php java python 总结

    paip.日期时间操作以及时间戳uapi php java python 总结 ///uapi Date 函数 | Day 函数 | Hour 函数 | Minute 函数 | Month 函数 | ...

  7. [时间操作] C#DateFormat时间帮助类 (转载)

    点击下载 DateFormat.rar 主要功能如下 返回每月的第一天和最后一天 看下面代码吧 /// <summary> /// 类说明:时间操作类 /// 编 码 人:苏飞 /// 联 ...

  8. Flex时间操作

    小弟是Flex新手,最近一段时间领导要求使用Flex开发B/S的一些项目,需要用到时间上的一些操作.上网查询一番好多人都说不好操作,有的甚至非常麻烦.基于此,小弟整理了一些关于Flex时间操作的经验, ...

  9. JAVA中的时间操作

    java中的时间操作不外乎这四种情况: 1.获取当前时间 2.获取某个时间的某种格式 3.设置时间 4.时间的运算 好,下面就针对这四种情况,一个一个搞定. 一.获取当前时间 有两种方式可以获得,第一 ...

随机推荐

  1. MyBatis if test 传入一个数字进行比较报错 There is no getter for property named 'userState' in 'class java.lang.Integer'

    在写MyBatis映射文件中,我要传入一个 int 类型的参数,在映射文件中用 'test' 中进行比较,我花了很长时间百度,发现都是不靠谱的方法,有把数字在比较时转成字符串用 equals 比较的. ...

  2. Substring(Codeforces-D-拓扑排序)

    D. Substring time limit per test 3 seconds memory limit per test 256 megabytes You are given a graph ...

  3. MyEclipse提示Errors occurred during the build

    最近在使用Extjs 在springsource Tool Suite运行时老是出现: Errors occurred during the build. Errors running builder ...

  4. 查漏补缺:进程间通信(IPC):FIFO

    1.FIFO FIFO,又称命名管道.不同于pipe管道的只能用于拥有共同祖先进程的两个进程间通信,因FIFO通过路径绑定,所以即使是不相关的进程间也可通过FIFO进行数据交换. FIFO是一种文件类 ...

  5. Oracle中的列转行实现字段拼接用例

    文章目录 Oracle中的列转行实现字段拼接 场景 在SQL使用过程中经常有这种需求:将某列字段拼接成in('XX','XX','XX','XX','XX','XX' ...)做为查询条件. 实现 s ...

  6. 阿里云ESC学生服务器搭建springboot项目生产环境(Mysql+JDK)不需要上传安装包

    嗯,之前服务器被挖矿的病毒弄的登录不进去了,所以联系了阿里云客服,提交工单,最后建议重置,所以我就重置了, 嗯,学习经验,docker如果懂的不是太多,不要随便云部署,都给别人挖矿了.   Mysql ...

  7. 使用python抓取美团商家信息

    抓取美团商家信息 import requests from bs4 import BeautifulSoup import json url = 'http://bj.meituan.com/' ur ...

  8. 微信小程序入门讲解

    微信小程序 注册 由于发文限制,请自行到微信公众平台注册 项目结构 project.config.json 配置文件(不需要动) app.json(用户配置) 路由pages window 整个程序样 ...

  9. this软拷贝详解

    <script> if( !Function.prototype.softBind ){ Function.prototype.softBind = function( obj ){ va ...

  10. H5页面通用头部设置

    见到很多人写H5页面都不设置头部,不忍直视,于是整理一篇文章,不定期更新,为了让自己显得专业一点,也为了方便自己复制粘贴 一般来说必须设置项 <!-- 页面编码 --> <meta ...