Commit f2dcfd12510ac1085e9f95fca0008a769e9189aa
1 parent
f3c3d3dd
根据创建时间定时删除指定表记录信息初版提交
Signed-off-by: TanYibin <5491541@qq.com>
Showing
6 changed files
with
737 additions
and
19 deletions
ant-design-vue-jeecg/src/views/system/QuartzJobList.vue
@@ -43,8 +43,7 @@ | @@ -43,8 +43,7 @@ | ||
43 | <a-dropdown v-if="selectedRowKeys.length > 0"> | 43 | <a-dropdown v-if="selectedRowKeys.length > 0"> |
44 | <a-menu slot="overlay"> | 44 | <a-menu slot="overlay"> |
45 | <a-menu-item key="1" @click="batchDel"> | 45 | <a-menu-item key="1" @click="batchDel"> |
46 | - <a-icon type="delete"/> | ||
47 | - 删除 | 46 | + <a-icon type="delete"/>删除 |
48 | </a-menu-item> | 47 | </a-menu-item> |
49 | </a-menu> | 48 | </a-menu> |
50 | <a-button style="margin-left: 8px"> 批量操作 | 49 | <a-button style="margin-left: 8px"> 批量操作 |
@@ -84,14 +83,12 @@ | @@ -84,14 +83,12 @@ | ||
84 | 83 | ||
85 | 84 | ||
86 | <span slot="action" slot-scope="text, record"> | 85 | <span slot="action" slot-scope="text, record"> |
87 | - <a @click="resumeJob(record)" v-if="record.status==-1">启动</a> | ||
88 | - <a @click="pauseJob(record)" v-if="record.status==0">停止</a> | ||
89 | - | ||
90 | - <a-divider type="vertical"/> | 86 | + <a @click="resumeJob(record)" v-if="record.status==-1">启动<a-divider type="vertical"/></a> |
87 | + <a @click="pauseJob(record)" v-if="record.status==0">停止<a-divider type="vertical"/></a> | ||
88 | + <a @click="executeImmediately(record)">执行一次<a-divider type="vertical"/></a> | ||
91 | <a-dropdown> | 89 | <a-dropdown> |
92 | <a class="ant-dropdown-link">更多 <a-icon type="down"/></a> | 90 | <a class="ant-dropdown-link">更多 <a-icon type="down"/></a> |
93 | <a-menu slot="overlay"> | 91 | <a-menu slot="overlay"> |
94 | - <a-menu-item><a @click="executeImmediately(record)">执行一次</a></a-menu-item> | ||
95 | <a-menu-item><a @click="handleEdit(record)">编辑</a></a-menu-item> | 92 | <a-menu-item><a @click="handleEdit(record)">编辑</a></a-menu-item> |
96 | <a-menu-item> | 93 | <a-menu-item> |
97 | <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)"> | 94 | <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)"> |
@@ -163,7 +160,7 @@ export default { | @@ -163,7 +160,7 @@ export default { | ||
163 | { | 160 | { |
164 | title: '参数', | 161 | title: '参数', |
165 | align: "center", | 162 | align: "center", |
166 | - width: 150, | 163 | + width: 170, |
167 | dataIndex: 'parameter', | 164 | dataIndex: 'parameter', |
168 | scopedSlots: {customRender: 'parameterRender'}, | 165 | scopedSlots: {customRender: 'parameterRender'}, |
169 | }, | 166 | }, |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/monitor/job/TableCleanTesk.java
0 → 100644
1 | +package org.jeecg.modules.wms.monitor.job; | ||
2 | + | ||
3 | +import java.lang.reflect.Field; | ||
4 | +import java.lang.reflect.ParameterizedType; | ||
5 | +import java.time.LocalDateTime; | ||
6 | +import java.time.temporal.ChronoUnit; | ||
7 | +import java.util.ArrayList; | ||
8 | +import java.util.List; | ||
9 | + | ||
10 | +import org.apache.ibatis.session.SqlSession; | ||
11 | +import org.jeecg.common.util.DateUtils; | ||
12 | +import org.jeecg.modules.wms.monitor.job.dto.TableCleanDto; | ||
13 | +import org.jeecg.utils.LocalDateUtils; | ||
14 | +import org.quartz.DisallowConcurrentExecution; | ||
15 | +import org.quartz.Job; | ||
16 | +import org.quartz.JobExecutionContext; | ||
17 | +import org.quartz.JobExecutionException; | ||
18 | +import org.quartz.PersistJobDataAfterExecution; | ||
19 | +import org.springframework.beans.factory.annotation.Autowired; | ||
20 | + | ||
21 | +import com.alibaba.fastjson.JSON; | ||
22 | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||
23 | +import com.baomidou.mybatisplus.core.mapper.BaseMapper; | ||
24 | + | ||
25 | +import cn.hutool.core.util.StrUtil; | ||
26 | +import dm.jdbc.util.ReflectUtil; | ||
27 | +import lombok.extern.slf4j.Slf4j; | ||
28 | + | ||
29 | +@Slf4j | ||
30 | +@PersistJobDataAfterExecution | ||
31 | +@DisallowConcurrentExecution | ||
32 | +public class TableCleanTesk implements Job { | ||
33 | + | ||
34 | + private String parameter; | ||
35 | + | ||
36 | + @Autowired | ||
37 | + private SqlSession sqlSession; | ||
38 | + | ||
39 | + public void setParameter(String parameter) { | ||
40 | + this.parameter = parameter; | ||
41 | + } | ||
42 | + | ||
43 | + @Override | ||
44 | + public void execute(JobExecutionContext context) throws JobExecutionException { | ||
45 | + try { | ||
46 | + log.info(StrUtil.format("定时任务 TableCleanTesk 参数:{},执行时间:{}", this.parameter, DateUtils.getTimestamp())); | ||
47 | + List<TableCleanDto> tableCleanList = JSON.parseArray(this.parameter, TableCleanDto.class); | ||
48 | + if (tableCleanList != null && tableCleanList.size() > 0) { | ||
49 | + for (TableCleanDto tableCleanDto : tableCleanList) { | ||
50 | + Class mapperClass = Class.forName(tableCleanDto.getMapperName()); | ||
51 | + ParameterizedType parameterizedType = (ParameterizedType)mapperClass.getGenericInterfaces()[0]; | ||
52 | + String className = parameterizedType.getActualTypeArguments()[0].getTypeName(); | ||
53 | + Field createTimeField = ReflectUtil.getField(Class.forName(className), "createTime"); | ||
54 | + if (createTimeField != null) { | ||
55 | + QueryWrapper<?> queryWrapper = new QueryWrapper<>(); | ||
56 | + LocalDateTime createTime = LocalDateUtils.minus(LocalDateTime.now(), tableCleanDto.getRetentionDays(), ChronoUnit.DAYS); | ||
57 | + queryWrapper.select("id").lt("create_time", createTime); | ||
58 | + BaseMapper baseMapper = (BaseMapper)this.sqlSession.getMapper(mapperClass); | ||
59 | + Integer deleteCount = baseMapper.delete(queryWrapper); | ||
60 | + log.info("定时任务 TableCleanTesk 删除 {} {} 天前数据 {} 行", className, tableCleanDto.getRetentionDays(), deleteCount); | ||
61 | + } | ||
62 | + } | ||
63 | + } | ||
64 | + } catch (ClassNotFoundException e) { | ||
65 | + e.printStackTrace(); | ||
66 | + } | ||
67 | + } | ||
68 | + | ||
69 | + public static void main(String[] args) { | ||
70 | + List<TableCleanDto> tableCleanList = new ArrayList<TableCleanDto>(); | ||
71 | + TableCleanDto tableCleanDto = new TableCleanDto(); | ||
72 | + tableCleanDto.setMapperName("org.jeecg.modules.system.mapper.SysLogMapper"); | ||
73 | + tableCleanDto.setRetentionDays(90); | ||
74 | + tableCleanDto.setMapperName("org.jeecg.modules.system.mapper.SysLogMapper"); | ||
75 | + tableCleanDto.setRetentionDays(90); | ||
76 | + tableCleanList.add(tableCleanDto); | ||
77 | + System.out.println(JSON.toJSONString(tableCleanList)); | ||
78 | + } | ||
79 | +} |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/monitor/job/dto/TableCleanDto.java
0 → 100644
1 | +package org.jeecg.modules.wms.monitor.job.dto; | ||
2 | + | ||
3 | +import org.jeecg.modules.wms.monitor.job.TableCleanTesk; | ||
4 | + | ||
5 | +import lombok.Data; | ||
6 | + | ||
7 | +@Data | ||
8 | +public class TableCleanDto { | ||
9 | + | ||
10 | + /** 需要清除的Mapper对象 */ | ||
11 | + private String mapperName; | ||
12 | + | ||
13 | + /** 保留天数 */ | ||
14 | + private Integer retentionDays; | ||
15 | + | ||
16 | +} | ||
0 | \ No newline at end of file | 17 | \ No newline at end of file |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/monitor/operation/entity/OperationLog.java
@@ -85,4 +85,9 @@ public class OperationLog implements Serializable { | @@ -85,4 +85,9 @@ public class OperationLog implements Serializable { | ||
85 | @Excel(name = "操作人姓名", width = 15) | 85 | @Excel(name = "操作人姓名", width = 15) |
86 | @ApiModelProperty(value = "操作人姓名") | 86 | @ApiModelProperty(value = "操作人姓名") |
87 | private java.lang.String operatorName; | 87 | private java.lang.String operatorName; |
88 | + | ||
89 | + /** 创建日期 */ | ||
90 | + @Excel(name = "创建日期", width = 15) | ||
91 | + @ApiModelProperty(value = "创建日期") | ||
92 | + private java.util.Date createTime; | ||
88 | } | 93 | } |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/monitor/operation/service/impl/OperationLogServiceImpl.java
@@ -120,17 +120,7 @@ public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, Ope | @@ -120,17 +120,7 @@ public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, Ope | ||
120 | } | 120 | } |
121 | } else { | 121 | } else { |
122 | Thread.sleep(1000); // 确保日志输出顺序 | 122 | Thread.sleep(1000); // 确保日志输出顺序 |
123 | - OperationLog operationLog = new OperationLog(); | ||
124 | - operationLog.setBizId(logDTO.getBizId()); | ||
125 | - operationLog.setOperationMsg(StringUtils.substring(logDTO.getMsg(), 0, 1000)); | ||
126 | - operationLog.setBizType(logDTO.getBizType()); | ||
127 | - operationLog.setBizTag(logDTO.getTag()); | ||
128 | - operationLog.setContentException(StringUtils.substring(logDTO.getException(), 0, 1000)); | ||
129 | - operationLog.setContentReturn(StringUtils.substring(logDTO.getReturnStr(), 0, 1000)); | ||
130 | - operationLog.setOperationCostTime(logDTO.getExecutionTime()); | ||
131 | - operationLog.setOperationStatus(logDTO.getSuccess().equals(true) ? 1 : 0); | ||
132 | - operationLog.setOperationTime(logDTO.getOperateDate()); | ||
133 | - operationLog.setOperatorName(logDTO.getOperatorId()); | 123 | + OperationLog operationLog = createOperationLog(logDTO); |
134 | operationLogList.add(operationLog); | 124 | operationLogList.add(operationLog); |
135 | } | 125 | } |
136 | return this.saveBatch(operationLogList); | 126 | return this.saveBatch(operationLogList); |
@@ -140,6 +130,21 @@ public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, Ope | @@ -140,6 +130,21 @@ public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, Ope | ||
140 | } | 130 | } |
141 | } | 131 | } |
142 | 132 | ||
133 | + private OperationLog createOperationLog(LogDTO logDTO) { | ||
134 | + OperationLog operationLog = new OperationLog(); | ||
135 | + operationLog.setBizId(logDTO.getBizId()); | ||
136 | + operationLog.setOperationMsg(StringUtils.substring(logDTO.getMsg(), 0, 1000)); | ||
137 | + operationLog.setBizType(logDTO.getBizType()); | ||
138 | + operationLog.setBizTag(logDTO.getTag()); | ||
139 | + operationLog.setContentException(StringUtils.substring(logDTO.getException(), 0, 1000)); | ||
140 | + operationLog.setContentReturn(StringUtils.substring(logDTO.getReturnStr(), 0, 1000)); | ||
141 | + operationLog.setOperationCostTime(logDTO.getExecutionTime()); | ||
142 | + operationLog.setOperationStatus(logDTO.getSuccess().equals(true) ? 1 : 0); | ||
143 | + operationLog.setOperationTime(logDTO.getOperateDate()); | ||
144 | + operationLog.setOperatorName(logDTO.getOperatorId()); | ||
145 | + return operationLog; | ||
146 | + } | ||
147 | + | ||
143 | private List<OperationLog> createOperationLogs(LogDTO logDTO, Class<?> clazz, String bizIdMethodName, String text, String... msgMethodNames) { | 148 | private List<OperationLog> createOperationLogs(LogDTO logDTO, Class<?> clazz, String bizIdMethodName, String text, String... msgMethodNames) { |
144 | List<OperationLog> operationLogList = new ArrayList<OperationLog>(); | 149 | List<OperationLog> operationLogList = new ArrayList<OperationLog>(); |
145 | List<?> detailList = JSON.parseArray(logDTO.getExtra(), clazz); | 150 | List<?> detailList = JSON.parseArray(logDTO.getExtra(), clazz); |
huaheng-wms-core/src/main/java/org/jeecg/utils/LocalDateUtils.java
0 → 100644
1 | +package org.jeecg.utils; | ||
2 | + | ||
3 | +import java.time.DayOfWeek; | ||
4 | +import java.time.LocalDate; | ||
5 | +import java.time.LocalDateTime; | ||
6 | +import java.time.LocalTime; | ||
7 | +import java.time.format.DateTimeFormatter; | ||
8 | +import java.time.temporal.ChronoUnit; | ||
9 | +import java.time.temporal.TemporalAccessor; | ||
10 | +import java.time.temporal.TemporalAdjusters; | ||
11 | +import java.util.ArrayList; | ||
12 | +import java.util.List; | ||
13 | + | ||
14 | +/** | ||
15 | + * Java8日期时间工具类 | ||
16 | + * @author JourWon | ||
17 | + * @date 2020/12/13 | ||
18 | + */ | ||
19 | +public class LocalDateUtils { | ||
20 | + | ||
21 | + /** | ||
22 | + * 显示年月日时分秒,例如 2015-08-11 09:51:53. | ||
23 | + */ | ||
24 | + public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; | ||
25 | + | ||
26 | + /** | ||
27 | + * 仅显示年月日,例如 2015-08-11. | ||
28 | + */ | ||
29 | + public static final String DATE_PATTERN = "yyyy-MM-dd"; | ||
30 | + | ||
31 | + /** | ||
32 | + * 仅显示时分秒,例如 09:51:53. | ||
33 | + */ | ||
34 | + public static final String TIME_PATTERN = "HH:mm:ss"; | ||
35 | + | ||
36 | + /** | ||
37 | + * 显示年月日时分秒(无符号),例如 20150811095153. | ||
38 | + */ | ||
39 | + public static final String UNSIGNED_DATETIME_PATTERN = "yyyyMMddHHmmss"; | ||
40 | + | ||
41 | + /** | ||
42 | + * 仅显示年月日(无符号),例如 20150811. | ||
43 | + */ | ||
44 | + public static final String UNSIGNED_DATE_PATTERN = "yyyyMMdd"; | ||
45 | + | ||
46 | + /** | ||
47 | + * 春天; | ||
48 | + */ | ||
49 | + public static final Integer SPRING = 1; | ||
50 | + | ||
51 | + /** | ||
52 | + * 夏天; | ||
53 | + */ | ||
54 | + public static final Integer SUMMER = 2; | ||
55 | + | ||
56 | + /** | ||
57 | + * 秋天; | ||
58 | + */ | ||
59 | + public static final Integer AUTUMN = 3; | ||
60 | + | ||
61 | + /** | ||
62 | + * 冬天; | ||
63 | + */ | ||
64 | + public static final Integer WINTER = 4; | ||
65 | + | ||
66 | + /** | ||
67 | + * 星期日; | ||
68 | + */ | ||
69 | + public static final String SUNDAY = "星期日"; | ||
70 | + | ||
71 | + /** | ||
72 | + * 星期一; | ||
73 | + */ | ||
74 | + public static final String MONDAY = "星期一"; | ||
75 | + | ||
76 | + /** | ||
77 | + * 星期二; | ||
78 | + */ | ||
79 | + public static final String TUESDAY = "星期二"; | ||
80 | + | ||
81 | + /** | ||
82 | + * 星期三; | ||
83 | + */ | ||
84 | + public static final String WEDNESDAY = "星期三"; | ||
85 | + | ||
86 | + /** | ||
87 | + * 星期四; | ||
88 | + */ | ||
89 | + public static final String THURSDAY = "星期四"; | ||
90 | + | ||
91 | + /** | ||
92 | + * 星期五; | ||
93 | + */ | ||
94 | + public static final String FRIDAY = "星期五"; | ||
95 | + | ||
96 | + /** | ||
97 | + * 星期六; | ||
98 | + */ | ||
99 | + public static final String SATURDAY = "星期六"; | ||
100 | + | ||
101 | + /** | ||
102 | + * 年 | ||
103 | + */ | ||
104 | + private static final String YEAR = "year"; | ||
105 | + | ||
106 | + /** | ||
107 | + * 月 | ||
108 | + */ | ||
109 | + private static final String MONTH = "month"; | ||
110 | + | ||
111 | + /** | ||
112 | + * 周 | ||
113 | + */ | ||
114 | + private static final String WEEK = "week"; | ||
115 | + | ||
116 | + /** | ||
117 | + * 日 | ||
118 | + */ | ||
119 | + private static final String DAY = "day"; | ||
120 | + | ||
121 | + /** | ||
122 | + * 时 | ||
123 | + */ | ||
124 | + private static final String HOUR = "hour"; | ||
125 | + | ||
126 | + /** | ||
127 | + * 分 | ||
128 | + */ | ||
129 | + private static final String MINUTE = "minute"; | ||
130 | + | ||
131 | + /** | ||
132 | + * 秒 | ||
133 | + */ | ||
134 | + private static final String SECOND = "second"; | ||
135 | + | ||
136 | + /** | ||
137 | + * 获取当前日期和时间字符串. | ||
138 | + * @return String 日期时间字符串,例如 2015-08-11 09:51:53 | ||
139 | + */ | ||
140 | + public static String getLocalDateTimeStr() { | ||
141 | + return format(LocalDateTime.now(), DATETIME_PATTERN); | ||
142 | + } | ||
143 | + | ||
144 | + /** | ||
145 | + * 获取当前日期字符串. | ||
146 | + * @return String 日期字符串,例如2015-08-11 | ||
147 | + */ | ||
148 | + public static String getLocalDateStr() { | ||
149 | + return format(LocalDate.now(), DATE_PATTERN); | ||
150 | + } | ||
151 | + | ||
152 | + /** | ||
153 | + * 获取当前时间字符串. | ||
154 | + * @return String 时间字符串,例如 09:51:53 | ||
155 | + */ | ||
156 | + public static String getLocalTimeStr() { | ||
157 | + return format(LocalTime.now(), TIME_PATTERN); | ||
158 | + } | ||
159 | + | ||
160 | + /** | ||
161 | + * 获取当前星期字符串. | ||
162 | + * @return String 当前星期字符串,例如 星期二 | ||
163 | + */ | ||
164 | + public static String getDayOfWeekStr() { | ||
165 | + return format(LocalDate.now(), "E"); | ||
166 | + } | ||
167 | + | ||
168 | + /** | ||
169 | + * 获取指定日期是星期几 | ||
170 | + * @param localDate 日期 | ||
171 | + * @return String 星期几 | ||
172 | + */ | ||
173 | + public static String getDayOfWeekStr(LocalDate localDate) { | ||
174 | + String[] weekOfDays = {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY}; | ||
175 | + int dayOfWeek = localDate.getDayOfWeek().getValue() - 1; | ||
176 | + return weekOfDays[dayOfWeek]; | ||
177 | + } | ||
178 | + | ||
179 | + /** | ||
180 | + * 获取日期时间字符串 | ||
181 | + * @param temporal 需要转化的日期时间 | ||
182 | + * @param pattern 时间格式 | ||
183 | + * @return String 日期时间字符串,例如 2015-08-11 09:51:53 | ||
184 | + */ | ||
185 | + public static String format(TemporalAccessor temporal, String pattern) { | ||
186 | + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); | ||
187 | + return dateTimeFormatter.format(temporal); | ||
188 | + } | ||
189 | + | ||
190 | + /** | ||
191 | + * 日期时间字符串转换为日期时间(java.time.LocalDateTime) | ||
192 | + * @param localDateTimeStr 日期时间字符串 | ||
193 | + * @param pattern 日期时间格式 例如DATETIME_PATTERN | ||
194 | + * @return LocalDateTime 日期时间 | ||
195 | + */ | ||
196 | + public static LocalDateTime parseLocalDateTime(String localDateTimeStr, String pattern) { | ||
197 | + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); | ||
198 | + return LocalDateTime.parse(localDateTimeStr, dateTimeFormatter); | ||
199 | + } | ||
200 | + | ||
201 | + /** | ||
202 | + * 日期字符串转换为日期(java.time.LocalDate) | ||
203 | + * @param localDateStr 日期字符串 | ||
204 | + * @param pattern 日期格式 例如DATE_PATTERN | ||
205 | + * @return LocalDate 日期 | ||
206 | + */ | ||
207 | + public static LocalDate parseLocalDate(String localDateStr, String pattern) { | ||
208 | + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); | ||
209 | + return LocalDate.parse(localDateStr, dateTimeFormatter); | ||
210 | + } | ||
211 | + | ||
212 | + /** | ||
213 | + * 获取指定日期时间加上指定数量日期时间单位之后的日期时间. | ||
214 | + * @param localDateTime 日期时间 | ||
215 | + * @param num 数量 | ||
216 | + * @param chronoUnit 日期时间单位 | ||
217 | + * @return LocalDateTime 新的日期时间 | ||
218 | + */ | ||
219 | + public static LocalDateTime plus(LocalDateTime localDateTime, int num, ChronoUnit chronoUnit) { | ||
220 | + return localDateTime.plus(num, chronoUnit); | ||
221 | + } | ||
222 | + | ||
223 | + /** | ||
224 | + * 获取指定日期时间减去指定数量日期时间单位之后的日期时间. | ||
225 | + * @param localDateTime 日期时间 | ||
226 | + * @param num 数量 | ||
227 | + * @param chronoUnit 日期时间单位 | ||
228 | + * @return LocalDateTime 新的日期时间 | ||
229 | + */ | ||
230 | + public static LocalDateTime minus(LocalDateTime localDateTime, int num, ChronoUnit chronoUnit) { | ||
231 | + return localDateTime.minus(num, chronoUnit); | ||
232 | + } | ||
233 | + | ||
234 | + /** | ||
235 | + * 根据ChronoUnit计算两个日期时间之间相隔日期时间 | ||
236 | + * @param start 开始日期时间 | ||
237 | + * @param end 结束日期时间 | ||
238 | + * @param chronoUnit 日期时间单位 | ||
239 | + * @return long 相隔日期时间 | ||
240 | + */ | ||
241 | + public static long getChronoUnitBetween(LocalDateTime start, LocalDateTime end, ChronoUnit chronoUnit) { | ||
242 | + return Math.abs(start.until(end, chronoUnit)); | ||
243 | + } | ||
244 | + | ||
245 | + /** | ||
246 | + * 根据ChronoUnit计算两个日期之间相隔年数或月数或天数 | ||
247 | + * @param start 开始日期 | ||
248 | + * @param end 结束日期 | ||
249 | + * @param chronoUnit 日期时间单位,(ChronoUnit.YEARS,ChronoUnit.MONTHS,ChronoUnit.WEEKS,ChronoUnit.DAYS) | ||
250 | + * @return long 相隔年数或月数或天数 | ||
251 | + */ | ||
252 | + public static long getChronoUnitBetween(LocalDate start, LocalDate end, ChronoUnit chronoUnit) { | ||
253 | + return Math.abs(start.until(end, chronoUnit)); | ||
254 | + } | ||
255 | + | ||
256 | + /** | ||
257 | + * 获取本年第一天的日期字符串 | ||
258 | + * @return String 格式:yyyy-MM-dd 00:00:00 | ||
259 | + */ | ||
260 | + public static String getFirstDayOfYearStr() { | ||
261 | + return getFirstDayOfYearStr(LocalDateTime.now()); | ||
262 | + } | ||
263 | + | ||
264 | + /** | ||
265 | + * 获取本年最后一天的日期字符串 | ||
266 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
267 | + */ | ||
268 | + public static String getLastDayOfYearStr() { | ||
269 | + return getLastDayOfYearStr(LocalDateTime.now()); | ||
270 | + } | ||
271 | + | ||
272 | + /** | ||
273 | + * 获取指定日期当年第一天的日期字符串 | ||
274 | + * @param localDateTime 指定日期时间 | ||
275 | + * @return String 格式:yyyy-MM-dd 00:00:00 | ||
276 | + */ | ||
277 | + public static String getFirstDayOfYearStr(LocalDateTime localDateTime) { | ||
278 | + return getFirstDayOfYearStr(localDateTime, DATETIME_PATTERN); | ||
279 | + } | ||
280 | + | ||
281 | + /** | ||
282 | + * 获取指定日期当年最后一天的日期字符串 | ||
283 | + * @param localDateTime 指定日期时间 | ||
284 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
285 | + */ | ||
286 | + public static String getLastDayOfYearStr(LocalDateTime localDateTime) { | ||
287 | + return getLastDayOfYearStr(localDateTime, DATETIME_PATTERN); | ||
288 | + } | ||
289 | + | ||
290 | + /** | ||
291 | + * 获取指定日期当年第一天的日期字符串,带日期格式化参数 | ||
292 | + * @param localDateTime 指定日期时间 | ||
293 | + * @param pattern 日期时间格式 | ||
294 | + * @return String 格式:yyyy-MM-dd 00:00:00 | ||
295 | + */ | ||
296 | + public static String getFirstDayOfYearStr(LocalDateTime localDateTime, String pattern) { | ||
297 | + return format(localDateTime.withDayOfYear(1).withHour(0).withMinute(0).withSecond(0), pattern); | ||
298 | + } | ||
299 | + | ||
300 | + /** | ||
301 | + * 获取指定日期当年最后一天的日期字符串,带日期格式化参数 | ||
302 | + * @param localDateTime 指定日期时间 | ||
303 | + * @param pattern 日期时间格式 | ||
304 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
305 | + */ | ||
306 | + public static String getLastDayOfYearStr(LocalDateTime localDateTime, String pattern) { | ||
307 | + return format(localDateTime.with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59), pattern); | ||
308 | + } | ||
309 | + | ||
310 | + /** | ||
311 | + * 获取本月第一天的日期字符串 | ||
312 | + * @return String 格式:yyyy-MM-dd 00:00:00 | ||
313 | + */ | ||
314 | + public static String getFirstDayOfMonthStr() { | ||
315 | + return getFirstDayOfMonthStr(LocalDateTime.now()); | ||
316 | + } | ||
317 | + | ||
318 | + /** | ||
319 | + * 获取本月最后一天的日期字符串 | ||
320 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
321 | + */ | ||
322 | + public static String getLastDayOfMonthStr() { | ||
323 | + return getLastDayOfMonthStr(LocalDateTime.now()); | ||
324 | + } | ||
325 | + | ||
326 | + /** | ||
327 | + * 获取指定日期当月第一天的日期字符串 | ||
328 | + * @param localDateTime 指定日期时间 | ||
329 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
330 | + */ | ||
331 | + public static String getFirstDayOfMonthStr(LocalDateTime localDateTime) { | ||
332 | + return getFirstDayOfMonthStr(localDateTime, DATETIME_PATTERN); | ||
333 | + } | ||
334 | + | ||
335 | + /** | ||
336 | + * 获取指定日期当月最后一天的日期字符串 | ||
337 | + * @param localDateTime 指定日期时间 | ||
338 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
339 | + */ | ||
340 | + public static String getLastDayOfMonthStr(LocalDateTime localDateTime) { | ||
341 | + return getLastDayOfMonthStr(localDateTime, DATETIME_PATTERN); | ||
342 | + } | ||
343 | + | ||
344 | + /** | ||
345 | + * 获取指定日期当月第一天的日期字符串,带日期格式化参数 | ||
346 | + * @param localDateTime 指定日期时间 | ||
347 | + * @return String 格式:yyyy-MM-dd 00:00:00 | ||
348 | + */ | ||
349 | + public static String getFirstDayOfMonthStr(LocalDateTime localDateTime, String pattern) { | ||
350 | + return format(localDateTime.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0), pattern); | ||
351 | + } | ||
352 | + | ||
353 | + /** | ||
354 | + * 获取指定日期当月最后一天的日期字符串,带日期格式化参数 | ||
355 | + * @param localDateTime 指定日期时间 | ||
356 | + * @param pattern 日期时间格式 | ||
357 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
358 | + */ | ||
359 | + public static String getLastDayOfMonthStr(LocalDateTime localDateTime, String pattern) { | ||
360 | + return format(localDateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59), pattern); | ||
361 | + } | ||
362 | + | ||
363 | + /** | ||
364 | + * 获取本周第一天的日期字符串 | ||
365 | + * @return String 格式:yyyy-MM-dd 00:00:00 | ||
366 | + */ | ||
367 | + public static String getFirstDayOfWeekStr() { | ||
368 | + return getFirstDayOfWeekStr(LocalDateTime.now()); | ||
369 | + } | ||
370 | + | ||
371 | + /** | ||
372 | + * 获取本周最后一天的日期字符串 | ||
373 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
374 | + */ | ||
375 | + public static String getLastDayOfWeekStr() { | ||
376 | + return getLastDayOfWeekStr(LocalDateTime.now()); | ||
377 | + } | ||
378 | + | ||
379 | + /** | ||
380 | + * 获取指定日期当周第一天的日期字符串,这里第一天为周一 | ||
381 | + * @param localDateTime 指定日期时间 | ||
382 | + * @return String 格式:yyyy-MM-dd 00:00:00 | ||
383 | + */ | ||
384 | + public static String getFirstDayOfWeekStr(LocalDateTime localDateTime) { | ||
385 | + return getFirstDayOfWeekStr(localDateTime, DATETIME_PATTERN); | ||
386 | + } | ||
387 | + | ||
388 | + /** | ||
389 | + * 获取指定日期当周最后一天的日期字符串,这里最后一天为周日 | ||
390 | + * @param localDateTime 指定日期时间 | ||
391 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
392 | + */ | ||
393 | + public static String getLastDayOfWeekStr(LocalDateTime localDateTime) { | ||
394 | + return getLastDayOfWeekStr(localDateTime, DATETIME_PATTERN); | ||
395 | + } | ||
396 | + | ||
397 | + /** | ||
398 | + * 获取指定日期当周第一天的日期字符串,这里第一天为周一,带日期格式化参数 | ||
399 | + * @param localDateTime 指定日期时间 | ||
400 | + * @param pattern 日期时间格式 | ||
401 | + * @return String 格式:yyyy-MM-dd 00:00:00 | ||
402 | + */ | ||
403 | + public static String getFirstDayOfWeekStr(LocalDateTime localDateTime, String pattern) { | ||
404 | + return format(localDateTime.with(DayOfWeek.MONDAY).withHour(0).withMinute(0).withSecond(0), pattern); | ||
405 | + } | ||
406 | + | ||
407 | + /** | ||
408 | + * 获取指定日期当周最后一天的日期字符串,这里最后一天为周日,带日期格式化参数 | ||
409 | + * @param localDateTime 指定日期时间 | ||
410 | + * @param pattern 日期时间格式 | ||
411 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
412 | + */ | ||
413 | + public static String getLastDayOfWeekStr(LocalDateTime localDateTime, String pattern) { | ||
414 | + return format(localDateTime.with(DayOfWeek.SUNDAY).withHour(23).withMinute(59).withSecond(59), pattern); | ||
415 | + } | ||
416 | + | ||
417 | + /** | ||
418 | + * 获取今天开始时间的日期字符串 | ||
419 | + * @return String 格式:yyyy-MM-dd 00:00:00 | ||
420 | + */ | ||
421 | + public static String getStartTimeOfDayStr() { | ||
422 | + return getStartTimeOfDayStr(LocalDateTime.now()); | ||
423 | + } | ||
424 | + | ||
425 | + /** | ||
426 | + * 获取今天结束时间的日期字符串 | ||
427 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
428 | + */ | ||
429 | + public static String getEndTimeOfDayStr() { | ||
430 | + return getEndTimeOfDayStr(LocalDateTime.now()); | ||
431 | + } | ||
432 | + | ||
433 | + /** | ||
434 | + * 获取指定日期开始时间的日期字符串 | ||
435 | + * @param localDateTime 指定日期时间 | ||
436 | + * @return String 格式:yyyy-MM-dd 00:00:00 | ||
437 | + */ | ||
438 | + public static String getStartTimeOfDayStr(LocalDateTime localDateTime) { | ||
439 | + return getStartTimeOfDayStr(localDateTime, DATETIME_PATTERN); | ||
440 | + } | ||
441 | + | ||
442 | + /** | ||
443 | + * 获取指定日期结束时间的日期字符串 | ||
444 | + * @param localDateTime 指定日期时间 | ||
445 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
446 | + */ | ||
447 | + public static String getEndTimeOfDayStr(LocalDateTime localDateTime) { | ||
448 | + return getEndTimeOfDayStr(localDateTime, DATETIME_PATTERN); | ||
449 | + } | ||
450 | + | ||
451 | + /** | ||
452 | + * 获取指定日期开始时间的日期字符串,带日期格式化参数 | ||
453 | + * @param localDateTime 指定日期时间 | ||
454 | + * @param pattern 日期时间格式 | ||
455 | + * @return String 格式:yyyy-MM-dd HH:mm:ss | ||
456 | + */ | ||
457 | + public static String getStartTimeOfDayStr(LocalDateTime localDateTime, String pattern) { | ||
458 | + return format(localDateTime.withHour(0).withMinute(0).withSecond(0), pattern); | ||
459 | + } | ||
460 | + | ||
461 | + /** | ||
462 | + * 获取指定日期结束时间的日期字符串,带日期格式化参数 | ||
463 | + * @param localDateTime 指定日期时间 | ||
464 | + * @param pattern 日期时间格式 | ||
465 | + * @return String 格式:yyyy-MM-dd 23:59:59 | ||
466 | + */ | ||
467 | + public static String getEndTimeOfDayStr(LocalDateTime localDateTime, String pattern) { | ||
468 | + return format(localDateTime.withHour(23).withMinute(59).withSecond(59), pattern); | ||
469 | + } | ||
470 | + | ||
471 | + /** | ||
472 | + * 切割日期。按照周期切割成小段日期段。例如: <br> | ||
473 | + * @param startDate 开始日期(yyyy-MM-dd) | ||
474 | + * @param endDate 结束日期(yyyy-MM-dd) | ||
475 | + * @param period 周期(天,周,月,年) | ||
476 | + * @return 切割之后的日期集合 | ||
477 | + * <li>startDate="2019-02-28",endDate="2019-03-05",period="day"</li> | ||
478 | + * <li>结果为:[2019-02-28, 2019-03-01, 2019-03-02, 2019-03-03, 2019-03-04, 2019-03-05]</li><br> | ||
479 | + * <li>startDate="2019-02-28",endDate="2019-03-25",period="week"</li> | ||
480 | + * <li>结果为:[2019-02-28,2019-03-06, 2019-03-07,2019-03-13, 2019-03-14,2019-03-20, | ||
481 | + * 2019-03-21,2019-03-25]</li><br> | ||
482 | + * <li>startDate="2019-02-28",endDate="2019-05-25",period="month"</li> | ||
483 | + * <li>结果为:[2019-02-28,2019-02-28, 2019-03-01,2019-03-31, 2019-04-01,2019-04-30, | ||
484 | + * 2019-05-01,2019-05-25]</li><br> | ||
485 | + * <li>startDate="2019-02-28",endDate="2020-05-25",period="year"</li> | ||
486 | + * <li>结果为:[2019-02-28,2019-12-31, 2020-01-01,2020-05-25]</li><br> | ||
487 | + */ | ||
488 | + public static List<String> listDateStrs(String startDate, String endDate, String period) { | ||
489 | + List<String> result = new ArrayList<>(); | ||
490 | + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN); | ||
491 | + LocalDate end = LocalDate.parse(endDate, dateTimeFormatter); | ||
492 | + LocalDate start = LocalDate.parse(startDate, dateTimeFormatter); | ||
493 | + LocalDate tmp = start; | ||
494 | + switch (period) { | ||
495 | + case DAY: | ||
496 | + while (start.isBefore(end) || start.isEqual(end)) { | ||
497 | + result.add(start.toString()); | ||
498 | + start = start.plusDays(1); | ||
499 | + } | ||
500 | + break; | ||
501 | + case WEEK: | ||
502 | + while (tmp.isBefore(end) || tmp.isEqual(end)) { | ||
503 | + if (tmp.plusDays(6).isAfter(end)) { | ||
504 | + result.add(tmp.toString() + "," + end); | ||
505 | + } else { | ||
506 | + result.add(tmp.toString() + "," + tmp.plusDays(6)); | ||
507 | + } | ||
508 | + tmp = tmp.plusDays(7); | ||
509 | + } | ||
510 | + break; | ||
511 | + case MONTH: | ||
512 | + while (tmp.isBefore(end) || tmp.isEqual(end)) { | ||
513 | + LocalDate lastDayOfMonth = tmp.with(TemporalAdjusters.lastDayOfMonth()); | ||
514 | + if (lastDayOfMonth.isAfter(end)) { | ||
515 | + result.add(tmp.toString() + "," + end); | ||
516 | + } else { | ||
517 | + result.add(tmp.toString() + "," + lastDayOfMonth); | ||
518 | + } | ||
519 | + tmp = lastDayOfMonth.plusDays(1); | ||
520 | + } | ||
521 | + break; | ||
522 | + case YEAR: | ||
523 | + while (tmp.isBefore(end) || tmp.isEqual(end)) { | ||
524 | + LocalDate lastDayOfYear = tmp.with(TemporalAdjusters.lastDayOfYear()); | ||
525 | + if (lastDayOfYear.isAfter(end)) { | ||
526 | + result.add(tmp.toString() + "," + end); | ||
527 | + } else { | ||
528 | + result.add(tmp.toString() + "," + lastDayOfYear); | ||
529 | + } | ||
530 | + tmp = lastDayOfYear.plusDays(1); | ||
531 | + } | ||
532 | + break; | ||
533 | + default: | ||
534 | + break; | ||
535 | + } | ||
536 | + return result; | ||
537 | + } | ||
538 | + | ||
539 | + public static void main(String[] args) { | ||
540 | + System.out.println(getLocalDateTimeStr()); | ||
541 | + System.out.println(getLocalDateStr()); | ||
542 | + System.out.println(getLocalTimeStr()); | ||
543 | + System.out.println(getDayOfWeekStr()); | ||
544 | + System.out.println(getDayOfWeekStr(LocalDate.now())); | ||
545 | + | ||
546 | + System.out.println("========"); | ||
547 | + System.out.println(format(LocalDate.now(), UNSIGNED_DATE_PATTERN)); | ||
548 | + | ||
549 | + System.out.println("========"); | ||
550 | + System.out.println(parseLocalDateTime("2020-12-13 11:14:12", DATETIME_PATTERN)); | ||
551 | + System.out.println(parseLocalDate("2020-12-13", DATE_PATTERN)); | ||
552 | +// | ||
553 | +// System.out.println("========"); | ||
554 | +// System.out.println(plus(LocalDateTime.now(), 3, ChronoUnit.HOURS)); | ||
555 | + System.out.println(format(minus(LocalDateTime.now(), 90, ChronoUnit.DAYS), DATETIME_PATTERN)); | ||
556 | + System.out.println(format(LocalDate.now(), UNSIGNED_DATE_PATTERN)); | ||
557 | +// System.out.println("========"); | ||
558 | +// System.out.println(getChronoUnitBetween(LocalDateTime.now(), parseLocalDateTime("2020-12-12 12:03:12", DATETIME_PATTERN), ChronoUnit.MINUTES)); | ||
559 | +// System.out.println(getChronoUnitBetween(LocalDate.now(), parseLocalDate("2021-12-12", DATE_PATTERN), ChronoUnit.WEEKS)); | ||
560 | +// | ||
561 | +// System.out.println("========"); | ||
562 | +// System.out.println(getFirstDayOfYearStr()); | ||
563 | +// System.out.println(getFirstDayOfYearStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); | ||
564 | +// System.out.println(getFirstDayOfYearStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); | ||
565 | +// | ||
566 | +// System.out.println(getLastDayOfYearStr()); | ||
567 | +// System.out.println(getLastDayOfYearStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); | ||
568 | +// System.out.println(getLastDayOfYearStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); | ||
569 | +// | ||
570 | +// System.out.println("========"); | ||
571 | +// System.out.println(getFirstDayOfMonthStr()); | ||
572 | +// System.out.println(getFirstDayOfMonthStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); | ||
573 | +// System.out.println(getFirstDayOfMonthStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); | ||
574 | +// | ||
575 | +// System.out.println(getLastDayOfMonthStr()); | ||
576 | +// System.out.println(getLastDayOfMonthStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); | ||
577 | +// System.out.println(getLastDayOfMonthStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); | ||
578 | +// | ||
579 | +// System.out.println("========"); | ||
580 | +// System.out.println(getFirstDayOfWeekStr()); | ||
581 | +// System.out.println(getFirstDayOfWeekStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); | ||
582 | +// System.out.println(getFirstDayOfWeekStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); | ||
583 | +// | ||
584 | +// System.out.println(getLastDayOfWeekStr()); | ||
585 | +// System.out.println(getLastDayOfWeekStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); | ||
586 | +// System.out.println(getLastDayOfWeekStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); | ||
587 | +// | ||
588 | +// System.out.println("========"); | ||
589 | +// System.out.println(getStartTimeOfDayStr()); | ||
590 | +// System.out.println(getStartTimeOfDayStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); | ||
591 | +// System.out.println(getStartTimeOfDayStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); | ||
592 | +// | ||
593 | +// System.out.println(getEndTimeOfDayStr()); | ||
594 | +// System.out.println(getEndTimeOfDayStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN))); | ||
595 | +// System.out.println(getEndTimeOfDayStr(parseLocalDateTime("2021-12-12 12:03:12", DATETIME_PATTERN), UNSIGNED_DATETIME_PATTERN)); | ||
596 | +// | ||
597 | +// System.out.println("========"); | ||
598 | +// List<String> dateStrs = listDateStrs("2019-01-30", "2020-12-13", YEAR); | ||
599 | +// for (String dateStr : dateStrs) { | ||
600 | +// System.out.println(dateStr); | ||
601 | +// } | ||
602 | +// | ||
603 | +// System.out.println("========"); | ||
604 | +// List<String> dateStrs1 = listDateStrs("2019-01-30", "2020-12-13", MONTH); | ||
605 | +// for (String dateStr : dateStrs1) { | ||
606 | +// System.out.println(dateStr); | ||
607 | +// } | ||
608 | +// | ||
609 | +// System.out.println("========"); | ||
610 | +// List<String> dateStrs2 = listDateStrs("2020-12-01", "2020-12-13", DAY); | ||
611 | +// for (String dateStr : dateStrs2) { | ||
612 | +// System.out.println(dateStr); | ||
613 | +// } | ||
614 | + } | ||
615 | + | ||
616 | +} |