近期在做一些首页的统计数据复习了下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. SSL (Secure Sockets Layer)

    本文转载自SSL (Secure Sockets Layer) TLS简介 The Transport Layer Security (TLS) protocol aims primarily to ...

  2. 1098 Insertion or Heap Sort——PAT甲级真题

    1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  3. 无情面试官之包含min函数的栈

    0 我是一个无情的面试官. 面人无数,挂人无数. 若想过我的面试,标准只有一个,那就是公司很缺人. 招新人,填旧坑. 1 今天是我的第1001次当面试官,要求却不是千里挑一,而是一击必中. 因为我招聘 ...

  4. docker数据卷的操作

    一般情况下会比较频繁的修改容器内部的文件 频繁docker cp 不太方便 使用数据卷可以将宿机的某个目录映射至容器的目录 修改会方便点 1.创建数据卷 docker volume create 数据 ...

  5. linux系统导出随笔

    导出时,不要用root用户忽略某张表的命令(多张表则直接往后加即可) --ignore-table=firewall_8088.operate_history --ignore-table=firew ...

  6. 基于Docker Compose部署分布式MinIO集群

    一.概述 Minio 是一个基于Go语言的对象存储服务.它实现了大部分亚马逊S3云存储服务接口,可以看做是是S3的开源版本,非常适合于存储大容量非结构化的数据,例如图片.视频.日志文件.备份数据和容器 ...

  7. linux的0,1,2号进程

    一.答案 https://blog.csdn.net/gatieme/article/details/51532804 仔细读一下作者的博客,都是操作系统底层相关. 二.补充: 1.linux代码,a ...

  8. 后端程序员之路 55、go redis

    redigo有点像hiredis,只提供了最基本的连接和执行命令接口. 找到个不错的redis库: https://github.com/go-redis/redis func ExampleNewC ...

  9. 后端程序员之路 51、A Tour of Go-1

    # A Tour of Go    - go get golang.org/x/tour/gotour    - https://tour.golang.org/    # welcome    - ...

  10. Python爬虫系统化学习(4)

    Python爬虫系统化学习(4) 在之前的学习过程中,我们学习了如何爬取页面,对页面进行解析并且提取我们需要的数据. 在通过解析得到我们想要的数据后,最重要的步骤就是保存数据. 一般的数据存储方式有两 ...