joda-time的简单使用及mysql时间函数的使用(今天,本周,本月)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

joda-time的简单使用及mysql时间函数的使用(今天,本周,本月)

西红柿炒番茄拌土豆   2021-04-20 我要评论

近期在做一些首页的统计数据复习了下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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们