近期在做一些首页的统计数据复习了下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更好的整体性能等等。

https://www.joda.org/joda-time/
 
1.java工具类实现规定时间
 
咱也只是使用不敢多说啥
直接上工具类(今天,本周,本月,前几月起始时间)
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");
}
}
 
2.mysql 函数实现
 
2.1 今日
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时间函数的使用(今天,本周,本月)的更多相关文章

  1. MySQL时间函数,用起来比PHP还爽

    前一篇写了PHP的时间函数(还是草稿),这一篇就写Mysql的时间函数吧.最近做的项目,关乎权限,于是自然而然的就与有效期联系在了一起.其中有一个功能是生成特殊表格,可以根据用户的选择,按周.月.季. ...

  2. Mysql时间函数

    http://blog.sina.com.cn/s/blog_6d39dc6f0100m7eo.html mysql中函数和关键字不区分大小写.下文函数的datetime参数处既可以用时间字符串也可以 ...

  3. mysql 时间函数 时间转换函数

    时间函数 Now 获取当前时间 current_timestamp 获取当前时间 localtimestamp 时间转换 UNIX_TIMESTAMP    "2009-09-15 00:0 ...

  4. mysql时间函数和时间操作

    补 原文链接:http://blog.csdn.net/yuxiayiji/article/details/7480785 select timediff('23:40:00', ' 18:30:00 ...

  5. MySQL 时间函数加减计算

    一.MySQL 获得当前日期时间 函数 1.1 获得当前日期 + 时间(date + time) 函数:now() mysql> select now();+———————+| now() |+ ...

  6. mysql 时间函数date_format

    http://toptree.iteye.com/blog/812642今天,在开发邮件系统的时候发现有很多的邮件没有发送成功,想手动把数据修改.找了mysql 的日期函数 获得的pubtime为St ...

  7. mysql时间函数操作

    Mysql时间转换函数 https://blog.csdn.net/w_qqqqq/article/details/88863269 mysql时间日期函数 https://www.cnblogs.c ...

  8. mysql 时间函数转换

    1 NOW() //当前时间 2 SYSDATE() //当前时间 3 CURRENT_TIMESTAMP 4 以'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式返回当前的 ...

  9. mysql时间函数,总是记不住,总是查。

    http://www.cnblogs.com/zeroone/archive/2010/05/05/1727659.html UNIX_TIMESTAMP() UNIX_TIMESTAMP(date) ...

随机推荐

  1. [转]hpp.h与.h的区别

    原文网址:https://blog.csdn.net/liuzhanchen1987/article/details/7270005 hpp,其实质就是将.cpp的实现代码混入.h头文件当中,定义与实 ...

  2. java的read方法

    public class RandomAccessDemo6 { public static void main(String[] args) throws IOException { RandomA ...

  3. 简单比较 @EnableEurekaClient 和 @EnableDiscoveryClient 两个注解

    @EnableEurekaClient 和 @EnableDiscoveryClient 都是让eureka发现该服务并注册到eureka的注解 相同点: 它们都能让注册中心Eureka发现 , 并将 ...

  4. cxf实例异常

    基于CXF2.3.0 Caused by: java.lang.InstantiationException: org.apache.cxf.wstx_msv_validation.WoodstoxV ...

  5. RabbitMQ之死信队列

    1:何为死信队列 死信队列也是一个正常的队列,可以被消费. 但是,死信队列的消息来源于其他队列的转发. 2:如何触发死信队列 1:消息超时 2:队列长度达到极限 3:消息被拒绝消费,并不再重进队列,且 ...

  6. Elasticsearch 7.x配置用户名密码访问 开启x-pack验证

    一.修改elasticsearch 配置文件 1.在配置文件中开启x-pack验证 #进入es安装目录下的config目录 vim elasticsearch.yml # 配置X-Pack http. ...

  7. mysql主从复制,主主复制,级联复制,半同步复制

    -------------------------------------------------------------------------------主从复制----------------- ...

  8. 后端程序员之路 59、go uiprogress

    gosuri/uiprogress: A go library to render progress bars in terminal applicationshttps://github.com/g ...

  9. Win10下ctrl与alt键互换

    我之前尝试过用第三方软件修改,但是总是不成功,后来发现直接去修改注册表也不麻烦,记录一下步骤. win + r 输入 regedit 进到这个路径 点击Keyboard Layout 右键,新建一个 ...

  10. 184. 部门工资最高的员工 + join + in

    184. 部门工资最高的员工 LeetCode_MySql_184 题目描述 题解分析 1.首先需要使用group by找出工资最高的值 2. 然后考虑到最高工资的可能有多位,所以使用in语句找到所有 ...