当前位置:网站首页>关于日期相加减问题
关于日期相加减问题
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();
} 
边栏推荐
猜你喜欢

0 basic self-study STM32 (wildfire) -- use register to light LED -- Explanation of GPIO function block diagram

sequential detector

How to solve MySQL 1045 error in Linux

【现代信号处理第六次作业】

与爱同行,育润走进贫困家庭,助推公益事业

0基础自学STM32(野火)——使用寄存器点亮LED——GPIO功能框图讲解

NVIDIA安装最新显卡驱动

0基础自学STM32(野火)——寄存器点亮LED

LSB hidden items of stream carrier based on assembly implementation

OpenFeign使用步骤 轮询策略与权重 log4j使用 openFeign拦截器的配置
随机推荐
OpenFeign使用步骤 轮询策略与权重 log4j使用 openFeign拦截器的配置
R语言将距离矩阵输入给hclust函数进行层次聚类分析,method参数指定两个组合数据点间的距离计算方式、plot函数可视化层次聚类的树状图(dendrogram)
What are the usage scenarios for locks in MySQL
First batch! Tencent cloud's ability to pass the solution of the government affairs collaboration platform of the China Academy of ICT
Development of freedom free agreement pledge mining system
Subgraphs in slam
mysql查询视图命令是哪个
【现代信号处理第六次作业】
0基础自学STM32(野火)——寄存器点亮LED
Master slave replication of MySQL
mysql支持外键吗
[the sixth operation of modern signal processing]
Custom handlerinterceptor interceptor for user authentication
LeetCode 每日一题——535. TinyURL 的加密与解密
Graduation season | Huawei experts teach interview tips: how to get a high salary offer from a large factory?
Can MySQL views create indexes
C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
epoll分析
关于harbor私有仓库忘记登录密码
How MySQL queries character set codes of tables