当前位置:网站首页>关于日期相加减问题
关于日期相加减问题
2022-06-29 17:31:00 【Wen先森】
MYSQL数据库中
addtime()
为日期加上指定秒数
select addtime(now(),1); -- 加1秒
adddate()
有两种用法,第二个参数直接填数字的话是为日期加上指定天数,填interval的话是为日期加上指定的interval时间。
select adddate(now(),1); -- 加1天
select adddate(now(), interval 1 day); -- 加1天
select adddate(now(), interval 1 hour); --加1小时
select adddate(now(), interval 1 minute); -- 加1分钟
select adddate(now(), interval 1 second); -- 加1秒
select adddate(now(), interval 1 microsecond); -- 加1毫秒
select adddate(now(), interval 1 week); -- 加1周
select adddate(now(), interval 1 month); -- 加1月
select adddate(now(), interval 1 quarter); -- 加1季
select adddate(now(), interval 1 year); -- 加1年
date_add()
为日期增加一个时间间隔,这个只能使用interval时间作为参数,用法和adddate()一致
select date_add(now(), interval 1 day); -- 加1天
select date_add(now(), interval 1 hour); -- 加1小时
select date_add(now(), interval 1 minute); -- 加1分钟
select date_add(now(), interval 1 second); -- 加1秒
select date_add(now(), interval 1 microsecond); -- 加1毫秒
select date_add(now(), interval 1 week); -- 加1周
select date_add(now(), interval 1 month); -- 加1月
select date_add(now(), interval 1 quarter); -- 加1季
select date_add(now(), interval 1 year); -- 加1年
subtime()
为日期减去指定秒数
select subtime(now(), 1); -- 减1秒
subdate()
与adddate()函数用法一致,有两种用法,第二个参数直接填数字的话是为日期减去指定天数,填interval的话是为日期减去指定的interval时间
select subdate(now(),1); -- 减1天
select subdate(now(), interval 1 day); -- 减1天
select subdate(now(), interval 1 hour); --减1小时
select subdate(now(), interval 1 minute); -- 减1分钟
select subdate(now(), interval 1 second); -- 减1秒
select subdate(now(), interval 1 microsecond); -- 减1毫秒
select subdate(now(), interval 1 week); -- 减1周
select subdate(now(), interval 1 month); -- 减1月
select subdate(now(), interval 1 quarter); -- 减1季
select subdate(now(), interval 1 year); -- 减1年
date_sub()
与date_add()函数用法一致,为日期减去一个时间间隔,这个只能使用interval时间作为参数
select date_sub(now(), interval 1 day); -- 减1天
select date_sub(now(), interval 1 hour); --减1小时
select date_sub(now(), interval 1 minute); -- 减1分钟
select date_sub(now(), interval 1 second); -- 减1秒
select date_sub(now(), interval 1 microsecond); -- 减1毫秒
select date_sub(now(), interval 1 week); -- 减1周
select date_sub(now(), interval 1 month); -- 减1月
select date_sub(now(), interval 1 quarter); -- 减1季
select date_sub(now(), interval 1 year); -- 减1年
示例:
select date_add(now(), interval 1 month) addTime,DATE_SUB(now(), interval 1 month) AS subTime

Java程序中
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println(sdf.format(date));
date = addDay(date, 3,-18);
System.out.println(sdf.format(date));
}
private static Date addDay(Date date, int days,int hour) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, days);
calendar.add(Calendar.HOUR_OF_DAY, hour);
return calendar.getTime();
} 
边栏推荐
- 自定義HandlerInterceptor攔截器實現用戶鑒權
- 【R语言数据科学】:文本挖掘(以特朗普推文数据为例)
- C language practice ---- pointer string and linked list
- How to create and delete MySQL triggers
- mysql视图能不能创建索引
- Development of freedom free agreement pledge mining system
- KUKA子程序/函数怎么建立和使用方法
- C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
- SRM系统可以为企业带来什么价值?
- R语言ggplot2可视化:使用patchwork包(直接使用加号+)将两个ggplot2可视化结果横向组合、接着再和第三个图像横向组合起来(三幅图各占比例为50%、25%、25%)
猜你喜欢

“授权同意”落地压力大?隐私计算提供一种可能的合规“技术解”

windows平台下的mysql启动等基本操作

Does MySQL support foreign keys

SLAM中的子图

How to solve the 2003 error of MySQL in Linux

LeetCode 每日一题——535. TinyURL 的加密与解密

Viewing splitchunks code segmentation from MPX resource construction optimization

controller、service、dao之间的关系

Bottom level internal skill cultivation

基于gis三维可视化的智慧城市行业运用
随机推荐
0基础自学STM32(野火)——使用寄存器点亮LED——GPIO功能框图讲解
Leetcode daily question - 535 Encryption and decryption of tinyurl
Redis 原理 - Sorted Set (ZSet)
The fixed assets management system enables enterprises to dynamically master assets
序列检测器
首批!腾讯云通过中国信通院政务协同平台解决方案能力评估
mysql在linux中2003错误如何解决
从居家办公中感悟适配器模式 | 社区征文
SAAS 服务的优势都有哪些
反射
Open source warehouse contribution - submit pr
一次采集JSON解析错误的修复
关于Go中两个模块互相调用的场景解决方案
腾讯云发布自动化交付和运维产品Orbit,推动企业应用全面云原生化
NVIDIA安装最新显卡驱动
reflex
SCM系统是什么?供应链管理系统有哪些优势?
Self taught structure (small turtle C language)
Online text digit recognition list summation tool
Interrupt怎么用