当前位置:网站首页>【时间的比较】
【时间的比较】
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。
边栏推荐
- 软考 --- 软件工程(1)概念、开发模型
- The strongest distributed lock tool: Redisson
- 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
- How ArkUI adapter somehow the screen
- 303. Range Sum Query - Immutable
- C语言03、数组
- CC2530_ZigBee+华为云IOT:设计一套属于自己的冷链采集系统
- C专家编程 第2章 这不是Bug,而是语言特性 2.2 多做之过
- LeetCode·72.编辑距离·动态规划
- SwinIR实战:如何使用SwinIR和预训练模型实现图片的超分
猜你喜欢
【指针初解】
Adobe是什么?
FinClip | July 2022 Product Highlights
沃尔沃:这是会“种草”的“安全感”!
Component communication - parent-child component communication
中小微企业如何简单便捷、低成本实现数字化?360视觉云有妙招
uniapp 切换 history 路由模
Description of the functional scenario of "collective storage and general governance" in the data center
为何微博又双叒叕崩溃了?
[Unity Starter Plan] Making RubyAdventure01 - Player Creation & Movement
随机推荐
【Metaverse系列一】元宇宙的奥秘
微信小程序 - 数组 push / unshift 追加后数组返回内容为数字(数组添加后打印结果为 Number 数值类型)
sphinx error connection to 127.0.0.1:9312 failed (errno=0, msg=)
sibling component communication context
请问下这个hologres维表是被缓存了么?怎么直接Finished了
Adobe是什么?
yolov5s用自己的数据集进行训练模型
11. Container With Most Water
C专家编程 第3章 分析C语言的声明 3.8 理解所有分析过程的代码段
组件通信--下拉菜单案例
组件通信-父传子组件通信
JS 字符串转 GBK 编码超精简实现
[Unity Getting Started Plan] Basic Concepts (7) - Input Manager & Input Class
J9数字虚拟论:元宇宙的潜力:一股推动社会进步的力量
C专家编程 第1章 C:穿越时空的迷雾 1.10 “安静的改变”究竟有多少安静
error:Illegal instruction (core dumped),离线下载安装这个other版本numpy
中小微企业如何简单便捷、低成本实现数字化?360视觉云有妙招
Detailed explanation of setting HiSilicon MMZ memory and OS memory
工程仪器设备在线监测管理系统常见问题和注意事项
融云「音视频架构实践」技术专场【内含完整PPT】