• Date日期工具类(数据库日期区间问题)



    前言

    在我们日常开发过程中,当涉及到处理日期和时间的操作时,字符串与Date日期类往往要经过相互转换,且在SQL语句的动态查询中,往往月份的格式不正确,SQL语句执行的效果是不同的:
    例如,如果我们想查询某年某月的所有订单,如果不能动态的获取到当月的天数信息(例如4月的区间查询必须是[1-30]),是查询不出来结果的,这时候就需要我们在后端动态的根据当月天数,查询当月有多少天,完成关于月份的动态查询:
    四月的订单有一条:
    在这里插入图片描述
    如果我们把区间单位设置为31,是查不到一条信息的:
    在这里插入图片描述
    区间在30就可以:
    在这里插入图片描述


    DateUtils日期工具类

    /**
     * 日期操作工具类
     */
    public class DateUtils {
        /**
         * 日期转换-  String -> Date
         *
         * @param dateString 字符串时间
         * @return Date类型信息
         * @throws Exception 抛出异常
         */
        public static Date parseString2Date(String dateString) throws Exception {
            if (dateString == null) {
                return null;
            }
            return parseString2Date(dateString, "yyyy-MM-dd");
        }
    
        /**
         * 日期转换-  String -> Date
         *
         * @param dateString 字符串时间
         * @param pattern    格式模板
         * @return Date类型信息
         * @throws Exception 抛出异常
         */
        public static Date parseString2Date(String dateString, String pattern) throws Exception {
            if (dateString == null) {
                return null;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Date date = sdf.parse(dateString);
            return date;
        }
    
        /**
         * 日期转换 Date -> String
         *
         * @param date Date类型信息
         * @return 字符串时间
         * @throws Exception 抛出异常
         */
        public static String parseDate2String(Date date) throws Exception {
            if (date == null) {
                return null;
            }
            return parseDate2String(date, "yyyy-MM-dd");
        }
    
        /**
         * 日期转换 Date -> String
         *
         * @param date    Date类型信息
         * @param pattern 格式模板
         * @return 字符串时间
         * @throws Exception 抛出异常
         */
        public static String parseDate2String(Date date, String pattern) throws Exception {
            if (date == null) {
                return null;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            String strDate = sdf.format(date);
            return strDate;
        }
    
        /**
         * 获取当前日期的本周一是几号
         *
         * @return 本周一的日期
         */
        public static Date getThisWeekMonday() {
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            // 获得当前日期是一个星期的第几天
            int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
            if (1 == dayWeek) {
                cal.add(Calendar.DAY_OF_MONTH, -1);
            }
            // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
            cal.setFirstDayOfWeek(Calendar.MONDAY);
            // 获得当前日期是一个星期的第几天
            int day = cal.get(Calendar.DAY_OF_WEEK);
            // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
            cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
            return cal.getTime();
        }
    
        /**
         * 获取当前日期周的最后一天
         *
         * @return 当前日期周的最后一天
         */
        public static Date getSundayOfThisWeek() {
            Calendar c = Calendar.getInstance();
            int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
            if (dayOfWeek == 0) {
                dayOfWeek = 7;
            }
            c.add(Calendar.DATE, -dayOfWeek + 7);
            return c.getTime();
        }
    
        /**
         * 根据日期区间获取月份列表
         *
         * @param minDate 开始时间
         * @param maxDate 结束时间
         * @return 月份列表
         * @throws Exception
         */
        public static List<String> getMonthBetween(String minDate, String maxDate, String format) throws Exception {
            ArrayList<String> result = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
    
            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
    
            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
            SimpleDateFormat sdf2 = new SimpleDateFormat(format);
    
            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf2.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
    
            return result;
        }
    
        /**
         * 根据日期获取年度中的周索引
         *
         * @param date 日期
         * @return 周索引
         * @throws Exception
         */
        public static Integer getWeekOfYear(String date) throws Exception {
            Date useDate = parseString2Date(date);
            Calendar cal = Calendar.getInstance();
            cal.setTime(useDate);
            return cal.get(Calendar.WEEK_OF_YEAR);
        }
    
        /**
         * 根据年份获取年中周列表
         *
         * @param year 年分
         * @return 周列表
         * @throws Exception
         */
        public static Map<Integer, String> getWeeksOfYear(String year) throws Exception {
            Date useDate = parseString2Date(year, "yyyy");
            Calendar cal = Calendar.getInstance();
            cal.setTime(useDate);
            //获取年中周数量
            int weeksCount = cal.getWeeksInWeekYear();
            Map<Integer, String> mapWeeks = new HashMap<>(55);
            for (int i = 0; i < weeksCount; i++) {
                cal.get(Calendar.DAY_OF_YEAR);
                mapWeeks.put(i + 1, parseDate2String(getFirstDayOfWeek(cal.get(Calendar.YEAR), i)));
            }
            return mapWeeks;
        }
    
        /**
         * 获取某年的第几周的开始日期
         *
         * @param year 年分
         * @param week 周索引
         * @return 开始日期
         * @throws Exception
         */
        public static Date getFirstDayOfWeek(int year, int week) throws Exception {
            Calendar c = new GregorianCalendar();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, Calendar.JANUARY);
            c.set(Calendar.DATE, 1);
    
            Calendar cal = (GregorianCalendar) c.clone();
            cal.add(Calendar.DATE, week * 7);
    
            return getFirstDayOfWeek(cal.getTime());
        }
    
        /**
         * 获取某年的第几周的结束日期
         *
         * @param year 年份
         * @param week 周索引
         * @return 结束日期
         * @throws Exception
         */
        public static Date getLastDayOfWeek(int year, int week) throws Exception {
            Calendar c = new GregorianCalendar();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, Calendar.JANUARY);
            c.set(Calendar.DATE, 1);
    
            Calendar cal = (GregorianCalendar) c.clone();
            cal.add(Calendar.DATE, week * 7);
    
            return getLastDayOfWeek(cal.getTime());
        }
    
        /**
         * 获取当前时间所在周的开始日期
         *
         * @param date 当前时间
         * @return 开始时间
         */
        public static Date getFirstDayOfWeek(Date date) {
            Calendar c = new GregorianCalendar();
            c.setFirstDayOfWeek(Calendar.SUNDAY);
            c.setTime(date);
            c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
            return c.getTime();
        }
    
        /**
         * 获取当前时间所在周的结束日期
         *
         * @param date 当前时间
         * @return 结束日期
         */
        public static Date getLastDayOfWeek(Date date) {
            Calendar c = new GregorianCalendar();
            c.setFirstDayOfWeek(Calendar.SUNDAY);
            c.setTime(date);
            c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
            return c.getTime();
        }
        //获得上周一的日期
        public static Date geLastWeekMonday(Date date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(getThisWeekMonday(date));
            cal.add(Calendar.DATE, -7);
            return cal.getTime();
        }
    
        //获得本周一的日期
        public static Date getThisWeekMonday(Date date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            // 获得当前日期是一个星期的第几天
            int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
            if (1 == dayWeek) {
                cal.add(Calendar.DAY_OF_MONTH, -1);
            }
            // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
            cal.setFirstDayOfWeek(Calendar.MONDAY);
            // 获得当前日期是一个星期的第几天
            int day = cal.get(Calendar.DAY_OF_WEEK);
            // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
            cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
            return cal.getTime();
        }
    
        //获得下周一的日期
        public static Date getNextWeekMonday(Date date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(getThisWeekMonday(date));
            cal.add(Calendar.DATE, 7);
            return cal.getTime();
        }
    
        //获得今天日期
        public static Date getToday(){
            return new Date();
        }
    
        //获得本月一日的日期
        public static Date getFirstDay4ThisMonth(){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH,1);
            return calendar.getTime();
        }
    
        public static void main(String[] args) {
            try {
                System.out.println("本周一" + parseDate2String(getThisWeekMonday()));
                System.out.println("本月一日" + parseDate2String(getFirstDay4ThisMonth()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
        //获取本月多少天
        public static int getmaxmonthdays(Date date){
            Calendar calendar = Calendar.getInstance();
            calendar.set(date.getYear(),date.getMonth(),date.getDate());
            return calendar.getActualMaximum(Calendar.DATE);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299

    总结

    由于DateUtils 工具类中的方法都是静态的,是属于类的,所有在使用的过程中只需要用类型.方法名调用即可(DateUtils .getmaxmonthdays(Date date)),该工具类为我们提供了大量关于Date的方法,封装调用,提升变成性能的同时也降低程序的耦合性。

  • 相关阅读:
    关于ssh的使用
    php webuploader 大文件上传带进度条
    “节省内存、提升性能:享元模式的神奇之处“
    flinksql读写redis
    Go--数据类型
    昇思 MindSpore 安装教程
    VMware虚拟机联网时自己遇到的所有问题 - 双端互ping - 虚拟机ping外网 - 安装10台以上虚拟机的经验总结
    java计算机毕业设计家庭安防系统源码+mysql数据库+系统+lw文档+部署
    re:从0开始的CSS之旅 16. 高度塌陷问题
    【CSDN线上竞赛9】小结
  • 原文地址:https://blog.csdn.net/HSQdePYZ/article/details/132805221