Procházet zdrojové kódy

添加日期工具类的方法

shengyang000 před 5 roky
rodič
revize
5650803ee2
3 změnil soubory, kde provedl 366 přidání a 0 odebrání
  1. 114 0
      csc-szls-common-utils.iml
  2. 4 0
      pom.xml
  3. 248 0
      src/main/java/com/unissoft/utils/DateUtil.java

+ 114 - 0
csc-szls-common-utils.iml

@@ -0,0 +1,114 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" version="4">
3
+  <component name="FacetManager">
4
+    <facet type="Spring" name="Spring">
5
+      <configuration />
6
+    </facet>
7
+    <facet type="web" name="Web">
8
+      <configuration>
9
+        <webroots />
10
+      </configuration>
11
+    </facet>
12
+  </component>
13
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
14
+    <output url="file://$MODULE_DIR$/target/classes" />
15
+    <output-test url="file://$MODULE_DIR$/target/test-classes" />
16
+    <content url="file://$MODULE_DIR$">
17
+      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
18
+      <excludeFolder url="file://$MODULE_DIR$/target" />
19
+    </content>
20
+    <orderEntry type="inheritedJdk" />
21
+    <orderEntry type="sourceFolder" forTests="false" />
22
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.1.2.RELEASE" level="project" />
23
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.1.2.RELEASE" level="project" />
24
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.1.2.RELEASE" level="project" />
25
+    <orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
26
+    <orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
27
+    <orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.11.1" level="project" />
28
+    <orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.11.1" level="project" />
29
+    <orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.25" level="project" />
30
+    <orderEntry type="library" name="Maven: javax.annotation:javax.annotation-api:1.3.2" level="project" />
31
+    <orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.23" level="project" />
32
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.1.2.RELEASE" level="project" />
33
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.8" level="project" />
34
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.0" level="project" />
35
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.8" level="project" />
36
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.8" level="project" />
37
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.8" level="project" />
38
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.9.8" level="project" />
39
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.1.2.RELEASE" level="project" />
40
+    <orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.14" level="project" />
41
+    <orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:9.0.14" level="project" />
42
+    <orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.14" level="project" />
43
+    <orderEntry type="library" name="Maven: org.hibernate.validator:hibernate-validator:6.0.14.Final" level="project" />
44
+    <orderEntry type="library" name="Maven: javax.validation:validation-api:2.0.1.Final" level="project" />
45
+    <orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.3.2.Final" level="project" />
46
+    <orderEntry type="library" name="Maven: com.fasterxml:classmate:1.4.0" level="project" />
47
+    <orderEntry type="library" name="Maven: org.springframework:spring-web:5.1.4.RELEASE" level="project" />
48
+    <orderEntry type="library" name="Maven: org.springframework:spring-beans:5.1.4.RELEASE" level="project" />
49
+    <orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.1.4.RELEASE" level="project" />
50
+    <orderEntry type="library" name="Maven: org.springframework:spring-aop:5.1.4.RELEASE" level="project" />
51
+    <orderEntry type="library" name="Maven: org.springframework:spring-context:5.1.4.RELEASE" level="project" />
52
+    <orderEntry type="library" name="Maven: org.springframework:spring-expression:5.1.4.RELEASE" level="project" />
53
+    <orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.1.2.RELEASE" level="project" />
54
+    <orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.1.2.RELEASE" level="project" />
55
+    <orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:2.1.2.RELEASE" level="project" />
56
+    <orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.4.0" level="project" />
57
+    <orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.3" level="project" />
58
+    <orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.2" level="project" />
59
+    <orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.4" level="project" />
60
+    <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
61
+    <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
62
+    <orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:3.11.1" level="project" />
63
+    <orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:2.23.4" level="project" />
64
+    <orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.9.7" level="project" />
65
+    <orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.9.7" level="project" />
66
+    <orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.6" level="project" />
67
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
68
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-library:1.3" level="project" />
69
+    <orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.5.0" level="project" />
70
+    <orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" />
71
+    <orderEntry type="library" name="Maven: org.springframework:spring-core:5.1.4.RELEASE" level="project" />
72
+    <orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.1.4.RELEASE" level="project" />
73
+    <orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.1.4.RELEASE" level="project" />
74
+    <orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.6.2" level="project" />
75
+    <orderEntry type="library" scope="TEST" name="Maven: javax.xml.bind:jaxb-api:2.3.1" level="project" />
76
+    <orderEntry type="library" scope="TEST" name="Maven: javax.activation:javax.activation-api:1.2.0" level="project" />
77
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-redis:2.1.2.RELEASE" level="project" />
78
+    <orderEntry type="library" name="Maven: org.springframework.data:spring-data-redis:2.1.4.RELEASE" level="project" />
79
+    <orderEntry type="library" name="Maven: org.springframework.data:spring-data-keyvalue:2.1.4.RELEASE" level="project" />
80
+    <orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.1.4.RELEASE" level="project" />
81
+    <orderEntry type="library" name="Maven: org.springframework:spring-tx:5.1.4.RELEASE" level="project" />
82
+    <orderEntry type="library" name="Maven: org.springframework:spring-oxm:5.1.4.RELEASE" level="project" />
83
+    <orderEntry type="library" name="Maven: org.springframework:spring-context-support:5.1.4.RELEASE" level="project" />
84
+    <orderEntry type="library" name="Maven: io.lettuce:lettuce-core:5.1.3.RELEASE" level="project" />
85
+    <orderEntry type="library" name="Maven: io.projectreactor:reactor-core:3.2.5.RELEASE" level="project" />
86
+    <orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.2" level="project" />
87
+    <orderEntry type="library" name="Maven: io.netty:netty-common:4.1.31.Final" level="project" />
88
+    <orderEntry type="library" name="Maven: io.netty:netty-transport:4.1.31.Final" level="project" />
89
+    <orderEntry type="library" name="Maven: io.netty:netty-buffer:4.1.31.Final" level="project" />
90
+    <orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.31.Final" level="project" />
91
+    <orderEntry type="library" name="Maven: io.netty:netty-handler:4.1.31.Final" level="project" />
92
+    <orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.31.Final" level="project" />
93
+    <orderEntry type="library" name="Maven: org.springframework.session:spring-session-data-redis:2.1.3.RELEASE" level="project" />
94
+    <orderEntry type="library" name="Maven: org.springframework.session:spring-session-core:2.1.3.RELEASE" level="project" />
95
+    <orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-boot-starter:3.4.1" level="project" />
96
+    <orderEntry type="library" name="Maven: com.baomidou:mybatis-plus:3.4.1" level="project" />
97
+    <orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-extension:3.4.1" level="project" />
98
+    <orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-core:3.4.1" level="project" />
99
+    <orderEntry type="library" name="Maven: com.baomidou:mybatis-plus-annotation:3.4.1" level="project" />
100
+    <orderEntry type="library" name="Maven: com.github.jsqlparser:jsqlparser:3.2" level="project" />
101
+    <orderEntry type="library" name="Maven: org.mybatis:mybatis:3.5.6" level="project" />
102
+    <orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:2.0.5" level="project" />
103
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.1.2.RELEASE" level="project" />
104
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.1.2.RELEASE" level="project" />
105
+    <orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.2.0" level="project" />
106
+    <orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.1.4.RELEASE" level="project" />
107
+    <orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.8.1" level="project" />
108
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-devtools:2.1.2.RELEASE" level="project" />
109
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.1.2.RELEASE" level="project" />
110
+    <orderEntry type="library" name="Maven: cn.hutool:hutool-all:5.3.6" level="project" />
111
+    <orderEntry type="library" name="Maven: org.json:json:20180813" level="project" />
112
+    <orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.37" level="project" />
113
+  </component>
114
+</module>

+ 4 - 0
pom.xml

@@ -44,6 +44,10 @@
44 44
             <groupId>com.baomidou</groupId>
45 45
             <artifactId>mybatis-plus-boot-starter</artifactId>
46 46
         </dependency>
47
+        <dependency>
48
+            <groupId>org.apache.commons</groupId>
49
+            <artifactId>commons-lang3</artifactId>
50
+        </dependency>
47 51
     </dependencies>
48 52
     <build>
49 53
         <plugins>

+ 248 - 0
src/main/java/com/unissoft/utils/DateUtil.java

@@ -1,5 +1,7 @@
1 1
 package com.unissoft.utils;
2 2
 
3
+import org.apache.commons.lang3.time.DateUtils;
4
+
3 5
 import java.text.Format;
4 6
 import java.text.ParseException;
5 7
 import java.text.SimpleDateFormat;
@@ -145,4 +147,250 @@ public class DateUtil {
145 147
         int second = (int) time / 1000;
146 148
         return second;
147 149
     }
150
+
151
+    /**
152
+     * 获取当前时间0秒的日期
153
+     * @return
154
+     */
155
+    public static Date getCurrMinute() {
156
+        Date date = new Date();
157
+
158
+        Calendar c = Calendar.getInstance();
159
+        c.setTime(date);
160
+        c.setLenient(true);
161
+
162
+        c.set(Calendar.SECOND, 0);
163
+        c.set(Calendar.MILLISECOND, 0);
164
+
165
+        return c.getTime();
166
+    }
167
+
168
+    /**
169
+     * 获取当前时间0分0秒的日期
170
+     * @return
171
+     */
172
+    public static Date getCurrHour() {
173
+        Date date = new Date();
174
+
175
+        Calendar c = Calendar.getInstance();
176
+        c.setTime(date);
177
+        c.setLenient(true);
178
+
179
+        c.set(Calendar.MINUTE, 0);
180
+        c.set(Calendar.SECOND, 0);
181
+        c.set(Calendar.MILLISECOND, 0);
182
+
183
+        return c.getTime();
184
+    }
185
+
186
+    /**
187
+     * 获取当前日期0时0分0秒的日期
188
+     * @return
189
+     */
190
+    public static Date getCurrDate() {
191
+        Date date = new Date();
192
+
193
+        Calendar c = Calendar.getInstance();
194
+        c.setTime(date);
195
+        c.setLenient(true);
196
+
197
+        c.set(Calendar.HOUR_OF_DAY, 0);
198
+        c.set(Calendar.MINUTE, 0);
199
+        c.set(Calendar.SECOND, 0);
200
+        c.set(Calendar.MILLISECOND, 0);
201
+
202
+        return c.getTime();
203
+    }
204
+
205
+    /**
206
+     * 获取当前周星期一0时0分0秒的日期
207
+     * @return
208
+     */
209
+    public static Date getFirstDateOfCurrWeek() {
210
+        Date date = new Date();
211
+
212
+        Calendar c = Calendar.getInstance();
213
+        c.setTime(date);
214
+        c.setLenient(true);
215
+
216
+        c.set(Calendar.DAY_OF_WEEK, 2);//星期一:2
217
+        c.set(Calendar.HOUR_OF_DAY, 0);
218
+        c.set(Calendar.MINUTE, 0);
219
+        c.set(Calendar.SECOND, 0);
220
+        c.set(Calendar.MILLISECOND, 0);
221
+
222
+        return c.getTime();
223
+    }
224
+
225
+    /**
226
+     * 获取当前月第一天0时0分0秒的日期
227
+     * @return
228
+     */
229
+    public static Date getFirstDateOfCurrMonth() {
230
+        Date date = new Date();
231
+
232
+        Calendar c = Calendar.getInstance();
233
+        c.setTime(date);
234
+        c.setLenient(true);
235
+
236
+        c.set(Calendar.DAY_OF_MONTH, 1);
237
+        c.set(Calendar.HOUR_OF_DAY, 0);
238
+        c.set(Calendar.MINUTE, 0);
239
+        c.set(Calendar.SECOND, 0);
240
+        c.set(Calendar.MILLISECOND, 0);
241
+
242
+        return c.getTime();
243
+    }
244
+
245
+    /**
246
+     * 获取当前季度第一天0时0分0秒的日期
247
+     * @return
248
+     */
249
+    public static Date getFirstDateOfCurrSeason() {
250
+        Date date = new Date();
251
+
252
+        Calendar c = Calendar.getInstance();
253
+        c.setTime(date);
254
+        c.setLenient(true);
255
+
256
+        int month = c.get(Calendar.MONTH);
257
+        c.set(Calendar.MONTH, month / 3 * 3);
258
+
259
+        c.set(Calendar.DAY_OF_MONTH, 1);
260
+        c.set(Calendar.HOUR_OF_DAY, 0);
261
+        c.set(Calendar.MINUTE, 0);
262
+        c.set(Calendar.SECOND, 0);
263
+        c.set(Calendar.MILLISECOND, 0);
264
+
265
+        return c.getTime();
266
+    }
267
+
268
+    /**
269
+     * 获取当前年第一天0时0分0秒的日期
270
+     * @return
271
+     */
272
+    public static Date getFirstDateOfCurrYear() {
273
+        Date date = new Date();
274
+
275
+        Calendar c = Calendar.getInstance();
276
+        c.setTime(date);
277
+        c.setLenient(true);
278
+
279
+        c.set(Calendar.DAY_OF_YEAR, 1);
280
+        c.set(Calendar.HOUR_OF_DAY, 0);
281
+        c.set(Calendar.MINUTE, 0);
282
+        c.set(Calendar.SECOND, 0);
283
+        c.set(Calendar.MILLISECOND, 0);
284
+
285
+        return c.getTime();
286
+    }
287
+
288
+
289
+
290
+
291
+    public static void main(String[] args) {
292
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
293
+//        System.out.println(sdf.format(getCurrMinute()));
294
+//        System.out.println(getCurrMinute());
295
+//
296
+//        System.out.println(sdf.format(getCurrHour()));
297
+//        System.out.println(getCurrHour());
298
+
299
+//        System.out.println(sdf.format(getCurrDate()));
300
+//        System.out.println(getCurrDate());
301
+//
302
+//        System.out.println(sdf.format(getFirstDateOfCurrWeek()));
303
+//        System.out.println(getFirstDateOfCurrWeek());
304
+//
305
+//        System.out.println(sdf.format(getFirstDateOfCurrMonth()));
306
+//        System.out.println(getFirstDateOfCurrMonth());
307
+//
308
+//        System.out.println(sdf.format(getFirstDateOfCurrSeason()));
309
+//        System.out.println(getFirstDateOfCurrSeason());
310
+//
311
+//        System.out.println(sdf.format(getFirstDateOfCurrYear()));
312
+//        System.out.println(getFirstDateOfCurrYear());
313
+//
314
+        System.out.println(sdf.format(aheadOfYears(new Date(), 2)));
315
+        System.out.println(sdf.format(aheadOfSeasons(new Date(), 1)));
316
+        System.out.println(sdf.format(aheadOfMonths(new Date(), 1)));
317
+
318
+//        System.out.println(sdf.format(aheadOfWeeks(new Date(), 2)));
319
+//        System.out.println(sdf.format(aheadOfDays(new Date(), 1)));
320
+//        System.out.println(sdf.format(aheadOfHours(new Date(), 3)));
321
+//        System.out.println(sdf.format(aheadOfMinutes(new Date(), 80)));
322
+
323
+//        Date date = evenMinuteDate(new Date());
324
+//        System.out.println(sdf.format(date));
325
+    }
326
+
327
+    /**
328
+     * 日期提前几年
329
+     * @param date
330
+     * @param years
331
+     * @return
332
+     */
333
+    public static Date aheadOfYears(Date date, int years) {
334
+        return DateUtils.addYears(date, 0 - years);
335
+    }
336
+
337
+    /**
338
+     * 日期提前几个季度
339
+     * @param date
340
+     * @param seasons
341
+     * @return
342
+     */
343
+    public static Date aheadOfSeasons(Date date, int seasons) {
344
+        return aheadOfMonths(date, 3 * seasons);
345
+    }
346
+
347
+    /**
348
+     * 日期提前几个月
349
+     * @param date
350
+     * @param months
351
+     * @return
352
+     */
353
+    public static Date aheadOfMonths(Date date, int months) {
354
+        return DateUtils.addMonths(date, 0 - months);
355
+    }
356
+
357
+    /**
358
+     * 日期提前几周
359
+     * @param date
360
+     * @param weeks
361
+     * @return
362
+     */
363
+    public static Date aheadOfWeeks(Date date, int weeks) {
364
+        return DateUtils.addWeeks(date, 0 - weeks);
365
+    }
366
+
367
+    /**
368
+     * 日期提前几天
369
+     * @param date
370
+     * @param days
371
+     * @return
372
+     */
373
+    public static Date aheadOfDays(Date date, int days) {
374
+        return DateUtils.addDays(date, 0 - days);
375
+    }
376
+
377
+    /**
378
+     * 日期提前几个小时
379
+     * @param date
380
+     * @param hours
381
+     * @return
382
+     */
383
+    public static Date aheadOfHours(Date date, int hours) {
384
+        return DateUtils.addHours(date, 0 - hours);
385
+    }
386
+
387
+    /**
388
+     * 日期提前几分钟
389
+     * @param date
390
+     * @param minutes
391
+     * @return
392
+     */
393
+    public static Date aheadOfMinutes(Date date, int minutes) {
394
+        return DateUtils.addMinutes(date, 0 - minutes);
395
+    }
148 396
 }