当前位置:网站首页>【时间的比较】
【时间的比较】
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。
边栏推荐
- ORACLE CLOUD 在国内有数据中心吗?
- 工程仪器设备在线监测管理系统常见问题和注意事项
- 【There is no tracking information for the current branch. Please specify which branch you want to 】
- EasyExcel实现动态列解析和存表
- 九种方法!教你如何读取resources目录下的文件路径
- Interviews are no longer hanged!This is the correct way to open the seven schemes of Redis distributed locks
- 数据万象内容审核 — 共建安全互联网,专项开展“清朗”直播整治行动
- 软考 --- 软件工程(1)概念、开发模型
- C专家编程 第3章 分析C语言的声明 3.5 typedef可以成为你的朋友
- 【Metaverse系列一】元宇宙的奥秘
猜你喜欢

附录A 程序员工作面试的秘密

组件通信-父传子组件通信

掌握Redis的Sentinel哨兵原理,可助你拿到25k的offer

EMQX Newsletter 2022-07|EMQX 5.0 正式发布、EMQX Cloud 新增 2 个数据库集成

2年开发经验去面试,吊打面试官,即将面试的程序员这些笔记建议复习

protobuf 中数据编码规则

The strongest distributed lock tool: Redisson

TiKV & TiFlash 加速复杂业务查询丨TiFlash 应用实践

Huawei, Lenovo, BAIC, etc. were selected as the first batch of training bases for "Enterprise Digital Transformation and Security Capability Improvement" by the Ministry of Industry and Information Te
![[Unity Getting Started Plan] Basic Concepts (7) - Input Manager & Input Class](/img/a7/950ddc6c9eeaa56fe0c3165d22a7d2.png)
[Unity Getting Started Plan] Basic Concepts (7) - Input Manager & Input Class
随机推荐
SQL中对 datetime 类型操作
使用Stream多年,collect还有这些“骚操作”?
deepstresam的插件配置说明,通过配置osd,设置字体的背景为透明
Auto Scaling 弹性伸缩(运维释放人力)
通俗理解apt-get 和pip的区别是什么
论文解读(JKnet)《Representation Learning on Graphs with Jumping Knowledge Networks》
[Unity Getting Started Plan] Basic Concepts (6) - Sprite Renderer Sprite Renderer
JS中对象数组用sort按属性排序
“68道 Redis+168道 MySQL”精品面试题(带解析),你背废了吗?
使用.NET简单实现一个Redis的高性能克隆版(一)
C语言02、语句、函数
请问下这个hologres维表是被缓存了么?怎么直接Finished了
TiKV & TiFlash 加速复杂业务查询丨TiFlash 应用实践
C专家编程 第3章 分析C语言的声明 3.3 优先级规则
【Metaverse系列一】元宇宙的奥秘
基于DMS的数仓智能运维服务,知多少?
【There is no tracking information for the current branch. Please specify which branch you want to 】
EMQX Newsletter 2022-07|EMQX 5.0 正式发布、EMQX Cloud 新增 2 个数据库集成
How to write SQL statements in DataWorks monitoring data reaches a certain value to indicate the change of
新特性解读 | MySQL 8.0 在线调整 REDO