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) ...
随机推荐
- OpenCVE-开源漏洞预警平台
0x01简介 主程序主要是通过使用NVD提供的JSON数据来更新CVE数据,并在前端进行展示.然后通过邮件进行通知,目前也只支持邮件.这个开源预警平台看上去并不是很完善,因为CVE本身就具有预警滞后性 ...
- redis和mysql结合数据一致性方案
缓存读: 缓存由于高并发高性能,已经被广泛的应用.在读取缓存方面做法一致.流程如下: 写缓存: 1.先更新数据库,再更新缓存 2.先更新数据库,再删除缓存. (1).先更新数据库,再更新缓存 这套方案 ...
- 实现TensorRT-7.0插件自由!(如果不踩坑使用TensorRT插件功能)
本系列为新TensorRT的第一篇,为什么叫新,因为之前已经写了两篇关于TensorRT的文章,是关于TensorRT-5.0版本的.好久没写关于TensorRT的文章了,所幸就以新来开头吧~ 接下来 ...
- 解决windwos系统80端口被暂用无法发布(NGINX、TOMCAT、IIS)
原因: 一个操作系统有0-65535个端口,但是一个端口只能被一个应用程序使用.所以80端口只有一个,当开发发布时想用应用NGINX,TOMCAT,IIS发布时,如果有程序占用了,就无法使用了. 解决 ...
- CSS布局,div居中,文字居中
.main { width: 100%; margin: 0 auto; .banner { img { width: 100%; } } .article { margin-bottom: 100p ...
- 在next主题添加微信公众号二维码
在侧边栏添加微信公众号二维码 首先,当然是准备一张微信公众号二维码.有两种添加方式,添加到侧边栏或者添加到推文的结尾处.我的next主题是7.x版本的,使用的主题是Gemini,设置的侧栏显示方式是一 ...
- git相关问题
1.git查看远程分支更新到本地 git clone 项目地址,示例如下: git clone https://github.com/zhongyushi-git/vue-test.git 在拉取时, ...
- JS中try catch的用法
在js中也可以使用try/catch语法,把可能发生异常的代码使用try包裹起来,然后在catch中对异常进行处理,处理后就不会影响后面代码的执行. const a = null try { cons ...
- css常用居中方式
一.水平居中 1.内联元素 父级元素加 text-align: center 即可 html <div class="container"> <a>内联元素 ...
- brew安装Nginx
目录 安装流程 常用命令记录 典型配置方式 查看启动状态是否有报错 php 启动 参考 安装流程 这里使用 brew 来安装软件. 安装 brew install nginx 查看安装信息(经常用到, ...