当前位置:网站首页>【时间的比较】
【时间的比较】
2022-08-03 17:01:00 【欧菲斯集团】
mysql中时间的比较方法
在mysql对数据的搜索中一定遇到过对时间的搜索。使用较多的有 <,>,<=,>=,between and。在搜索的过程中我们可以发现,对于在日期类型为DATE,DATETIME,TIMESTAMP,TIME的字符串进行比较的时候,我们比较的字符串无论是:
SELECT * from customer WHERE create_time < ‘2022-02-13’;
SELECT * from customer WHERE create_time < ‘2022#02#13’;
SELECT * from customer WHERE create_time < ‘20220213’;
都可以搜索出同样的数据结果。这是因为对这些类型进行比较的时候对字符串的格式要求不严格,比较的字符串可以选择任何格式的分割符进行比较。
获取当前时间
在进行比较的时候有时候会有种业务场景是要求获取当前时间,常用的有三类:
- 获取当前时间(日期+时间)
SELECT now();
SELECT sysdate();
SELECT CURRENT_TIMESTAMP();
前两函数获取的时间是datetime类型,而后一种获取的是时间戳。
- 获取当前日期(日期)
SELECT CURRENT_DATE();
获取的类型是date
- 获取当前时间(时间)
SELECT CURRENT_TIME();
获取的时间类型是time
Java中时间的比较方法
在Java中时间的比较方式就更多了,除了前面讲的mysql中的方法也是适用外还有mybatis提供的gt、ge、lt、le和Date()自带的after()、before()还有String提供的compareTo()都可由用于时间的比较。
gt、ge、lt、le
| 方法 | 作用 |
|---|---|
| gt | 大于> |
| ge | 大于等于> |
| lt | 小于< |
| le | 小于等于<= |
这些方法不仅仅只是用于时间的比较,他们还可以用于数量大小的比较。
after()和before()
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String beginTime="2022-07-09 10:22:22";
String endTime="2022-07-31 11:22:22";
Date beginDate=sdf.parse(beginTime);
Date endDate=sdf.parse(endTime);
System.out.println(beginDate.before(endDate));
System.out.println(beginDate.after(endDate));

compareTo()
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String beginTime="2022-07-09 10:22:22";
String endTime="2022-07-31 11:22:22";
Integer i=beginTime.compareTo(endTime);
System.out.println(i);
beginTime比endTime小时会返回负数,相反就返回正数,相等返回0。
边栏推荐
- 怎么在opengauss中进行测试自己添加的新函数的性能(循环n次的运行时间)?
- 使用Stream多年,collect还有这些“骚操作”?
- Which thread pool does Async use?
- 超分重建数据集
- C专家编程 第3章 分析C语言的声明 3.9 轻松一下---驱动物理实体的软件
- C语言03、数组
- 面试突击71:GET 和 POST 有什么区别?
- Description of the functional scenario of "collective storage and general governance" in the data center
- phoenix创建映射表和创建索引、删除索引
- MobileVIT实战:使用MobileVIT实现图像分类
猜你喜欢

LeetCode·1163.按字典序排在最后的子串·最小表示法

phoenix创建映射表和创建索引、删除索引

使用Stream多年,collect还有这些“骚操作”?

Description of the functional scenario of "collective storage and general governance" in the data center

After using Stream for many years, does collect still have these "saucy operations"?

Understand the recommendation system in one article: Outline 02: The link of the recommendation system, from recalling rough sorting, to fine sorting, to rearranging, and finally showing the recommend

设置海思芯片MMZ内存、OS内存详解

沃尔沃:这是会“种草”的“安全感”!

浅谈Service&nbsp;Mesh对业务系统的价值

error:Illegal instruction (core dumped),离线下载安装这个other版本numpy
随机推荐
sphinx coreseek的安装和php下使用
为什么我用了Redis之后,系统的性能却没有提升
Looking at the ecological potential of Hongmeng OS from the evolution of MatePad Pro
面试突击:什么是粘包和半包?怎么解决?
工程仪器设备在线监测管理系统常见问题和注意事项
Async的线程池使用的哪个?
102. 最佳牛围栏
#yyds干货盘点# 面试必刷TOP101:两个链表的第一个公共结点
高效的组织信息共享知识库是一种宝贵的资源
node connection mongoose database process
通用型安全监测数据管理系统
高薪程序员&面试题精讲系列132之微服务之间如何进行通信?服务熔断是怎么回事?你熟悉Hystrix吗?
自动化部署+整合SSM项目
被误解的 MVC 和被神化的 MVVM(二)
通俗理解apt-get 和pip的区别是什么
Which thread pool does Async use?
error:Illegal instruction (core dumped),离线下载安装这个other版本numpy
EasyExcel implements dynamic column parsing and table storage
出海,是泡泡玛特的“解药”吗?
“68道 Redis+168道 MySQL”精品面试题(带解析),你背废了吗?