joda-time的简单使用及mysql时间函数的使用(今天,本周,本月)
近期在做一些首页的统计数据复习了下mysql的时间函数,以及后续修改成 传入时间查询时使用的joda-time
软件简介
JodaTime 提供了一组Java类包用于处理包括ISO8601标准在内的date和time。可以利用它把JDK Date和Calendar类完全替换掉,而且仍然能够提供很好的集成。
Joda-Time主要的特点包括:
1. 易于使用:Calendar让获取"正常的"的日期变得很困难,使它没办法提供简单的方法,而Joda-Time能够 直接进行访问域并且索引值1就是代表January。
2. 易于扩展:JDK支持多日历系统是通过Calendar的子类来实现,这样就显示的非常笨重而且事实 上要实现其它日历系统是很困难的。Joda-Time支持多日历系统是通过基于Chronology类的插件体系来实现。
3. 提供一组完整的功能:它打算提供 所有关系到date-time计算的功能.Joda-Time当前支持6种日历系统,而且在将来还会继续添加。有着比JDK Calendar更好的整体性能等等。
public class JodaTimeUtils { public static String getThisWeekEndTime() {
DateTime now = DateTime.now();
now = now.withDayOfWeek(7)
.withHourOfDay(23)
.withMinuteOfHour(59)
.withSecondOfMinute(59); //本周日
return DateFormatUtils.format(now.toDate(), "yyyy-MM-dd HH:mm:ss");
} public static String getThisWeekStartTime() {
DateTime now = DateTime.now();
now = now.withDayOfWeek(1)
.withHourOfDay(0)
.withMinuteOfHour(0)
.withSecondOfMinute(0);
//本周1
return DateFormatUtils.format(now.toDate(), "yyyy-MM-dd HH:mm:ss");
} public static String getThisDayStartTime() {
DateTime now = DateTime.now();
now = now.millisOfDay()
.withMinimumValue();
//今天
return DateFormatUtils.format(now.toDate(), "yyyy-MM-dd HH:mm:ss");
} public static String getThisDayEndTime() {
DateTime now = DateTime.now();
now = now.millisOfDay()
.withMaximumValue();
//今天
return DateFormatUtils.format(now.toDate(), "yyyy-MM-dd HH:mm:ss");
} public static String getThisMonthStartTime() {
DateTime now = DateTime.now();
now = now.dayOfMonth().withMinimumValue()
.withHourOfDay(0)
.withMinuteOfHour(0)
.withSecondOfMinute(0); //本月初
return DateFormatUtils.format(now.toDate(), "yyyy-MM-dd HH:mm:ss");
} public static String getThisMonthEndTime() {
DateTime now = DateTime.now();
now = now.dayOfMonth().withMaximumValue()
.withHourOfDay(23)
.withMinuteOfHour(59)
.withSecondOfMinute(59);
//本月末
return DateFormatUtils.format(now.toDate(), "yyyy-MM-dd HH:mm:ss");
} // i 0 本月1上一个月 类推
public static String getMonthStartTime(int i) {
DateTime now = DateTime.now();
now = now.minusMonths(i).dayOfMonth().withMinimumValue()
.withHourOfDay(0)
.withMinuteOfHour(0)
.withSecondOfMinute(0); //本月初
return DateFormatUtils.format(now.toDate(), "yyyy-MM-dd HH:mm:ss");
} public static String getMonthEndTime(int i) {
DateTime now = DateTime.now();
now = now.minusMonths(i).dayOfMonth().withMaximumValue()
.withHourOfDay(23)
.withMinuteOfHour(59)
.withSecondOfMinute(59);
//本月末
return DateFormatUtils.format(now.toDate(), "yyyy-MM-dd HH:mm:ss");
}
}
TO_DAYS(create_time) = TO_DAYS(NOW())
2.2 本周
这里有个1是因为与国外周的起始不一样我国是周一国外是周天 所以多1
YEARWEEK(date_format(create_time,'%Y-%m-%d'), 1) = YEARWEEK(now(), 1)
2.3 本月
DATE_FORMAT( create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
2.4近几月
i 最小为0 指本月 1的话就上个月类推 如果你想搞个下个月 -1 也是支持的
date_format(create_time, '%Y %m') = date_format(DATE_SUB(curdate(), INTERVAL "+ i +" MONTH),'%Y %m')
分享到此
如有问题麻烦大佬告知
感谢 !!!
2021-04-20 16:09:34
joda-time的简单使用及mysql时间函数的使用(今天,本周,本月)的更多相关文章
- MySQL时间函数,用起来比PHP还爽
前一篇写了PHP的时间函数(还是草稿),这一篇就写Mysql的时间函数吧.最近做的项目,关乎权限,于是自然而然的就与有效期联系在了一起.其中有一个功能是生成特殊表格,可以根据用户的选择,按周.月.季. ...
- Mysql时间函数
http://blog.sina.com.cn/s/blog_6d39dc6f0100m7eo.html mysql中函数和关键字不区分大小写.下文函数的datetime参数处既可以用时间字符串也可以 ...
- mysql 时间函数 时间转换函数
时间函数 Now 获取当前时间 current_timestamp 获取当前时间 localtimestamp 时间转换 UNIX_TIMESTAMP "2009-09-15 00:0 ...
- mysql时间函数和时间操作
补 原文链接:http://blog.csdn.net/yuxiayiji/article/details/7480785 select timediff('23:40:00', ' 18:30:00 ...
- MySQL 时间函数加减计算
一.MySQL 获得当前日期时间 函数 1.1 获得当前日期 + 时间(date + time) 函数:now() mysql> select now();+———————+| now() |+ ...
- mysql 时间函数date_format
http://toptree.iteye.com/blog/812642今天,在开发邮件系统的时候发现有很多的邮件没有发送成功,想手动把数据修改.找了mysql 的日期函数 获得的pubtime为St ...
- mysql时间函数操作
Mysql时间转换函数 https://blog.csdn.net/w_qqqqq/article/details/88863269 mysql时间日期函数 https://www.cnblogs.c ...
- mysql 时间函数转换
1 NOW() //当前时间 2 SYSDATE() //当前时间 3 CURRENT_TIMESTAMP 4 以'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式返回当前的 ...
- mysql时间函数,总是记不住,总是查。
http://www.cnblogs.com/zeroone/archive/2010/05/05/1727659.html UNIX_TIMESTAMP() UNIX_TIMESTAMP(date) ...
随机推荐
- Markdown简单语法的使用
Markdown简单语法的使用 目录 Markdown简单语法的使用 前言 标题的设置 字体的设置 1.字体加粗 2.斜体 3.字体加粗斜体 3.删除线 引用 列表的使用 插入图片 分割线 代码的书写 ...
- [转]Ubuntu16 压缩解压文件命令
原文地址:http://blog.csdn.net/feibendexiaoma/article/details/73739279,转载主要方便随时查阅,如有版权要求,请及时联系. ZIP zip是比 ...
- Maven的-pl -am -amd参数
本文转载自Maven的-pl -am -amd参数学习 昨天maven的deploy任务需要只选择单个模块并且把它依赖的模块一起打包,第一时间便想到了-pl参数,然后就开始处理,但是因为之前只看了一下 ...
- 阿里云linux安装nginx,亲测有效
系统平台:CentOS release 6.6 (Final) 64位. 一.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtoo ...
- 手把手教你Spring Boot整合Mybatis Plus和Swagger2
前言:如果你是初学者,请完全按照我的教程以及代码来搭建(文末会附上完整的项目代码包,你可以直接下载我提供的完整项目代码包然后自行体验!),为了照顾初学者所以贴图比较多,请耐心跟着教程来,希望这个项目D ...
- 关于电脑硬盘的二三事(SATA接口)
@ 目录 前言 接口分类 SATA3接口 机械硬盘 机械硬盘的特点和主要参数 西部数据机械盘分类 绿·蓝·黑盘 红盘 紫盘 金盘 希捷机械盘分类 酷狼 酷鱼 酷鹰 银河 SATA3接口的固态硬盘 固态 ...
- Python爬虫学习笔记(四)
Request: Test1(基本属性:POST): 代码1: import requests # 发送POST请求 data = { } response = requests.post(url, ...
- three.js cannon.js物理引擎之约束(二)
今天郭先生继续讲cannon.js的物理约束,之前的一篇文章曾简单的提及过PointToPointConstraint约束,那么今天详细的说一说cannon.js的约束和使用方法.在线案例请点击博客原 ...
- HDR(高动态范围)
一: 简介 一般来说,当存储在帧缓冲(Framebuffer)中时,亮度和颜色的值是默认被限制在0.0到1.0之间的. 但是如果我们遇上了一个特定的区域,其中有多个亮光源使这些数值总和超过了1.0,又 ...
- Ubuntu 18.04下Intel SGX应用程序程序开发——获得OCALL调用的返回值
本文中,我们介绍在Enclave函数中调用不可信OCALL函数,并获得OCALL函数的返回值. 1. 复制SampleEnclave示例并建立自己的OcallRetSum项目 SampleEnclav ...