Loading...

有补充的欢迎大家帮我补充

  • list去除中括号
  • 时间转换(过去了多长时间)
  • 判断参数是不是为空或者为null
  • 模糊搜索(加强)
  • 正则判断是否包含大小写
  • 判断手机号格式是否正确
  • 获取IP地址
  • 获取mac地址
  • 获取uuid 32位 64位
  • 判断是否包含
  • 数组分页
  • 数组去重
  • 身份证获取性别
  • 生日判断年龄
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 个人工具类
*
* @author zhujx
* @date 2019-09-18 17:07
**/
@Slf4j
public class PersonalUtils {
/**
* list去除中括号
*
* @param list 数组
* @return java.lang.String
* @author zhujx
* @date 2019/7/3
*/
public static String idsToString(List<Long> list) {
return StringUtils.join(list, ",");
}

/**
* 时间转换
*
* @param time 时间戳
* @return java.lang.String
* @author zhujx
* @date 2019/8/6
*/
public static String stampToDate(Long time) {
time = time / 1000;
String dateTimes;
long days = time / (60 * 60 * 24);
long hours = (time % (60 * 60 * 24)) / (60 * 60);
long minutes = (time % (60 * 60)) / 60;
long seconds = time % 60;
if (days > 0) {
dateTimes = days + "天" + hours + "小时" + minutes + "分钟";
} else if (hours > 0) {
dateTimes = hours + "小时" + minutes + "分钟";
} else if (minutes > 0) {
dateTimes = minutes + "分钟";
} else {
dateTimes = seconds + "秒";
}
return dateTimes;
}

/**
* 判断参数
*
* @param obj 参数
* @return boolean
* @author zhujx
* @date 2019/8/14
*/
public static boolean isBlank(Object... obj) {
boolean isTrue = false;
for (Object o : obj) {
if (o == null) {
return true;
} else if (o instanceof String) {
isTrue = isTrue || StringUtils.isBlank(o.toString());
} else if (o instanceof List) {
isTrue = isTrue || ((List) o).isEmpty();
}
}
return isTrue;
}

/**
* 模糊搜索sql
*
* @param name 参数
* @return java.lang.String
* @author zhujx
* @date 2019/8/23
*/
public static String getLikeName(String name) {
if (isBlank(name)) {
return null;
} else {
return "%" + name + "%";
}
}

/**
* 加强搜索sql
*
* @param name 参数
* @return java.lang.String
* @author zhujx
* @date 2019/8/23
*/
public static String getLikeNameUp(String name) {
if (isBlank(name)) {
return null;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < name.length(); i++) {
sb.append("%");
sb.append(name.charAt(i));
}
return sb.append("%").toString();
}

private static final Pattern PATTERN_UP = Pattern.compile("^.*[A-Z]+.*$");
private static final Pattern PATTERN_LOW = Pattern.compile("^.*[a-z]+.*$");

/**
* 正则表达式判断是否包括大小写
*
* @param password 密码
* @return boolean
* @author zhujx
* @date 2019/8/23
*/
public static boolean pattern(String password) {
Matcher matcher = PATTERN_UP.matcher(password);
Matcher matcher1 = PATTERN_LOW.matcher(password);
return matcher.matches() && matcher1.matches();
}

private static final Pattern MOBILE = Pattern.compile("^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$");

/**
* 判断手机号是否正确
*
* @param phone 手机号
* @return boolean
* @author zhujx
* @date 2019/9/18
*/
public static boolean isPhone(String phone) throws Exception {
int mobileSize = 11;
if (phone.length() != mobileSize) {
throw new Exception("手机号应为11位数!");
} else {
Matcher m = MOBILE.matcher(phone);
boolean isMatch = m.matches();
if (!isMatch) {
throw new Exception("请填入正确的手机号!");
} else {
return true;
}
}
}

/**
* 获取ip
*
* @param request request
* @return java.lang.String
* @author zhujx
* @date 2020/12/14 13:47
*/
public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if (ip.contains(",")) {
return ip.split(",")[0];
} else {
return ip;
}
}

/**
* 获得32长度的UUID字符串
*
* @return java.lang.String
* @author zhujx
* @date 2020/4/20
*/
public static String getUuid32() {
return UUID.randomUUID().toString().replace("-", "");
}

/**
* 获得64长度的UUID字符串
*
* @return java.lang.String
* @author zhujx
* @date 2020/4/20
*/
public static String getUuid64() {
return getUuid32() + getUuid32();
}

/**
* 获取本机的mac地址 windows版
*
* @return java.lang.String
* @author zhujx
* @date 2021/3/5 10:28
*/
public static String getLocalMac() throws Exception {
InetAddress ia = InetAddress.getLocalHost();
//获取网卡,获取地址
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
//字节转换为整数
int temp = mac[i] & 0xff;
String str = Integer.toHexString(temp);
if (str.length() == 1) {
sb.append("0").append(str);
} else {
sb.append(str);
}
}
return sb.toString().toUpperCase();
}

/**
* 判断包含
*
* @param value 值
* @param values 值数组
* @return boolean
* @author zhujx
* @date 2019/12/18
*/
public static boolean isEquals(Object value, Object... values) {
boolean isTrue = false;
if (value != null) {
for (Object obj : values) {
if (value instanceof Integer || value instanceof Long || value instanceof String) {
isTrue = isTrue || value.equals(obj);
}
}
}
return isTrue;
}

/**
* 分页
*
* @param list list
* @param pageNum 当前页
* @param pageSize 每页条数
* @return java.util.List<T>
* @author zhujx
* @date 2020/1/8
*/
public static <T> List<T> getPage(List<T> list, int pageNum, int pageSize) {
int total = list.size();
int start = (pageNum - 1) * pageSize;
int end = pageNum * pageSize;
List<T> newList = new ArrayList<>();
for (int i = start; i < (Math.min(end, total)); i++) {
newList.add(list.get(i));
}
return newList;
}

/**
* 去重
*
* @param oldList 原数组
* @param newList 新数组
* @return java.util.List<T>
* @author zhujx
* @date 2020/2/15
*/
public static <T> List<T> removeList(List<T> oldList, List<T> newList) {
List<T> list = new ArrayList<>();
if (oldList.isEmpty() && newList.isEmpty()) {
return list;
} else if (oldList.isEmpty()) {
return newList;
} else if (newList.isEmpty()) {
return list;
} else {
for (T newT : newList) {
boolean isTrue = true;
for (T t : oldList) {
if (t.equals(newT)) {
isTrue = false;
break;
}
}
if (isTrue) {
list.add(newT);
}
}
}
return list;
}

/**
* 18位身份证获取性别
*
* @param idCard 身份证号码
* @return java.lang.String
* @author zhujx
* @date 2020/7/10 10:21
*/
public static String getSexByIdCard(String idCard) {
if (StringUtils.isBlank(idCard)) {
return null;
}
if (Integer.parseInt(idCard.substring(16).substring(0, 1)) % 2 == 0) {
return "女";
} else {
return "男";
}
}

/**
* 生日获取年龄
*
* @param birth 年月日
* @param pattern 格式 yyyy-MM-dd...
* @return java.lang.Integer
* @author zhujx
* @date 2020/7/10 10:21
*/
public static Integer getAgeByBirth(String birth, String pattern) {
if (StringUtils.isBlank(birth) || StringUtils.isBlank(pattern)) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Calendar cal = Calendar.getInstance();
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
try {
Date birthDate = sdf.parse(birth);
cal.setTime(birthDate);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
int age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth) {
age--;
}
} else {
age--;
}
}
return age;
} catch (ParseException e) {
log.error("getAgeByBirth date error birth = {} ,pattern = {}", birth, pattern);
return null;
}
}
}