網站首頁 編程語言 正文
獲取當前時間
- 獲取當前時間戳
select unix_timestamp()
- 把時間戳轉為正常的日期
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')
select from_unixtime(unix_timestamp(),'yyyy-MM-dd')
- 業務中有時存放的是包含毫秒的整數,需要先轉換為秒
select from_unixtime(cast(create_time/1000 as bigint),'yyyyMMdd') as dt
- 返回當天三種方式
SELECT CURRENT_DATE; --2017-06-15
select current_date(); -- 2021-10-22
SELECT current_timestamp; --返回時分秒
--2018-06-18 10:37:53.278
SELECT from_unixtime(unix_timestamp());
--2017-06-15 19:55:04
日期格式轉換
- 日期格式轉換 yyyyMMdd—>yyyy-MM-dd
select from_unixtime(unix_timestamp('20211022','yyyyMMdd'),"yyyy-MM-dd");
2021-10-22
- 固定日期轉換成時間戳
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
類似sql 中的date
- 截取日期部分
select substr('2021-10-22 17:34:56',1,10)
2021-10-22
select date_format('2021-10-22 17:34:56','yyyy-MM-dd')
2021-10-22
返回日期中的年,月,日,時,分,秒,當前的周數
- 返回日期中的年
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 date_sub(current_date(),1);
2021-10-21
最近一個月/30天
select date_sub(current_date(),30);
2021-09-22
- 前一日12點/昨日12點
select concat(date_format(date_sub(current_date(),1),'yyyy-MM-dd'),' ','12');
2021-10-21 12
返回當月或當年的第一天
- 返回當月的第一天
select trunc('2016-08-16','MM') --2016-08-01
select date_format(to_date(trunc(current_date(),'MM')),"yyyy-MM-dd");
2021-10-01
- 返回當年的第一天
select trunc('2016-08-16','YEAR') --2016-01-01
參考匯總
固定日期轉換成時間戳
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
類似sql 中的date
取當前時間
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
?參考鏈接:
Hive日期格式轉換方法總結
原文鏈接:https://cloud.tencent.com/developer/article/2013699
相關推薦
- 2023-07-16 spring boot 定時任務
- 2022-09-24 opencv實現圖像傾斜校正_C 語言
- 2022-09-20 一文詳解C++中動態內存管理_C 語言
- 2023-09-17 Android 獲取Wifi列表詳解(包含動態權限申請)
- 2021-12-11 關于docker容器部署redis步驟介紹_docker
- 2022-08-19 InnoDB 事務
- 2022-10-25 Git中smart?Checkout與force?checkout的區別及說明_相關技巧
- 2022-09-23 C語言中static的使用方法實例詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支