1. package org.springblade.common.utils;
    2. import org.springblade.core.tool.utils.Func;
    3. import java.math.BigDecimal;
    4. import java.math.RoundingMode;
    5. import java.text.DateFormat;
    6. import java.text.DecimalFormat;
    7. import java.text.SimpleDateFormat;
    8. import java.time.Instant;
    9. import java.time.LocalDateTime;
    10. import java.time.ZoneId;
    11. import java.util.*;
    12. import java.util.regex.Pattern;
    13. /**
    14. * What:高频常用方法集合<br>
    15. * Where:xx.xxx简短快捷的输入操作<br>
    16. * Why:整合高频常用方法,编码速度+50%,代码量-70%
    17. *
    18. */
    19. public class XX {
    20. /**
    21. * 对象是否为空
    22. *
    23. * @param o String,List,Map,Object[],int[],long[]
    24. * @return
    25. */
    26. @SuppressWarnings("rawtypes")
    27. public static boolean isEmpty(Object o) {
    28. if (o == null) {
    29. return true;
    30. }
    31. if (o instanceof String) {
    32. if (o.toString().trim().equals("")) {
    33. return true;
    34. }
    35. } else if (o instanceof List) {
    36. if (((List) o).size() == 0) {
    37. return true;
    38. }
    39. } else if (o instanceof Map) {
    40. if (((Map) o).size() == 0) {
    41. return true;
    42. }
    43. } else if (o instanceof Set) {
    44. if (((Set) o).size() == 0) {
    45. return true;
    46. }
    47. } else if (o instanceof Object[]) {
    48. if (((Object[]) o).length == 0) {
    49. return true;
    50. }
    51. } else if (o instanceof int[]) {
    52. if (((int[]) o).length == 0) {
    53. return true;
    54. }
    55. } else if (o instanceof long[]) {
    56. if (((long[]) o).length == 0) {
    57. return true;
    58. }
    59. } else if (o instanceof Integer) {
    60. if (o.toString().trim().equals("0")) {
    61. return true;
    62. }
    63. }
    64. return false;
    65. }
    66. /**
    67. * 对象组中是否存在 Empty Object
    68. *
    69. * @param os 对象组
    70. * @return
    71. */
    72. public static boolean isOneEmpty(Object... os) {
    73. for (Object o : os) {
    74. if (isEmpty(o)) {
    75. return true;
    76. }
    77. }
    78. return false;
    79. }
    80. /**
    81. * 对象组中是否全是 Empty Object
    82. *
    83. * @param os
    84. * @return
    85. */
    86. public static boolean isAllEmpty(Object... os) {
    87. for (Object o : os) {
    88. if (!isEmpty(o)) {
    89. return false;
    90. }
    91. }
    92. return true;
    93. }
    94. /**
    95. * 是否为数字
    96. *
    97. * @param obj
    98. * @return
    99. */
    100. public static boolean isNum(Object obj) {
    101. try {
    102. Integer.parseInt(obj.toString());
    103. } catch (Exception e) {
    104. return false;
    105. }
    106. return true;
    107. }
    108. /**
    109. * 字符串是否为 true
    110. *
    111. * @param str
    112. * @return
    113. */
    114. public static boolean isTrue(Object str) {
    115. if (isEmpty(str)) {
    116. return false;
    117. }
    118. str = str.toString().trim().toLowerCase();
    119. if (str.equals("true") || str.equals("on")) {
    120. return true;
    121. }
    122. return false;
    123. }
    124. /**
    125. * 格式化字符串->'str'
    126. *
    127. * @param str
    128. * @return
    129. */
    130. public static String format(Object str) {
    131. return "'" + str.toString() + "'";
    132. }
    133. /**
    134. * 强转->java.util.Date
    135. *
    136. * @param str 日期字符串
    137. * @return
    138. */
    139. public static Date toDate(String str) {
    140. try {
    141. if (str == null || "".equals(str.trim()))
    142. return null;
    143. return new SimpleDateFormat("yyyy-MM-dd").parse(str.trim());
    144. } catch (Exception e) {
    145. throw new RuntimeException("Can not parse the parameter \"" + str + "\" to Date value.");
    146. }
    147. }
    148. //yyyy-MM-dd HH:mm:ss转date
    149. // public static Date strToDate(String strDate) {
    150. // SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    151. // ParsePosition pos = new ParsePosition(0);
    152. // Date strtodate = formatter.parse(strDate, pos);
    153. // return strtodate;
    154. // }
    155. /**
    156. * Array转字符串(用指定符号分割)
    157. *
    158. * @param array
    159. * @param sign
    160. * @return
    161. */
    162. public static String join(Object[] array, char sign) {
    163. if (array == null) {
    164. return null;
    165. }
    166. int arraySize = array.length;
    167. int bufSize = (arraySize == 0 ? 0 : ((array[0] == null ? 16 : array[0].toString().length()) + 1) * arraySize);
    168. StringBuilder sb = new StringBuilder(bufSize);
    169. for (int i = 0; i < arraySize; i++) {
    170. if (i > 0) {
    171. sb.append(sign);
    172. }
    173. if (array[i] != null) {
    174. sb.append(array[i]);
    175. }
    176. }
    177. return sb.toString();
    178. }
    179. /**
    180. * 删除末尾字符串
    181. *
    182. * @param str 待处理字符串
    183. * @param sign 需要删除的符号
    184. * @return
    185. */
    186. public static String delEnd(String str, String sign) {
    187. if (str.endsWith(sign)) {
    188. return str.substring(0, str.lastIndexOf(sign));
    189. }
    190. return str;
    191. }
    192. /**
    193. * 消耗毫秒数
    194. *
    195. * @param time
    196. */
    197. public static void costTime(long time) {
    198. System.err.println("Load Cost Time:" + (System.currentTimeMillis() - time) + "ms\n");
    199. }
    200. /**
    201. * 格式化输出JSON
    202. *
    203. * @param json
    204. * @return
    205. */
    206. public static String formatJson(String json) {
    207. int level = 0;
    208. StringBuffer sb = new StringBuffer();
    209. for (int i = 0; i < json.length(); i++) {
    210. char c = json.charAt(i);
    211. if (level > 0 && '\n' == sb.charAt(sb.length() - 1)) {
    212. sb.append(getLevelStr(level));
    213. }
    214. switch (c) {
    215. case '{':
    216. case '[':
    217. sb.append(c + "\n");
    218. level++;
    219. break;
    220. case ',':
    221. sb.append(c + "\n");
    222. break;
    223. case '}':
    224. case ']':
    225. sb.append("\n");
    226. level--;
    227. sb.append(getLevelStr(level));
    228. sb.append(c);
    229. break;
    230. default:
    231. sb.append(c);
    232. break;
    233. }
    234. }
    235. return sb.toString();
    236. }
    237. private static String getLevelStr(int level) {
    238. StringBuffer levelStr = new StringBuffer();
    239. for (int levelI = 0; levelI < level; levelI++) {
    240. levelStr.append(" ");
    241. }
    242. return levelStr.toString();
    243. }
    244. /**
    245. * 获取当前时间
    246. *
    247. * @param formater 格式
    248. * @return
    249. */
    250. public static String getFormatTime(String formater, String time) {
    251. Date date = StrToDate(time);
    252. DateFormat format = new SimpleDateFormat(formater);
    253. return format.format(date);
    254. }
    255. public static String getFormatTime(String formater, Date time) {
    256. DateFormat format = new SimpleDateFormat(formater);
    257. return format.format(time);
    258. }
    259. public static Date StrToDate(String str) {
    260. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    261. Date date = null;
    262. try {
    263. date = format.parse(str);
    264. } catch (Exception e) {
    265. e.printStackTrace();
    266. }
    267. return date;
    268. }
    269. public static String DateToStr(Date date) {
    270. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    271. String str = null;
    272. if (Func.isNotEmpty(date)){
    273. str=format.format(date);
    274. }
    275. return str;
    276. }
    277. /**
    278. * 获取当前时间:yyyy-MM-dd HH:mm:ss
    279. *
    280. * @return
    281. */
    282. public static String getCurrentTime() {
    283. Date date = new Date();
    284. DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    285. return format.format(date);
    286. }
    287. public static String dateToStr(Date date) {
    288. if(date == null){
    289. return null;
    290. }else{
    291. DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    292. return format.format(date);
    293. }
    294. }
    295. /**
    296. * 获取当前日期:yyyy-MM-dd
    297. *
    298. * @return
    299. */
    300. public static String getCurrentDate() {
    301. Date date = new Date();
    302. DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    303. return format.format(date);
    304. }
    305. /**
    306. * 获取当前时间
    307. *
    308. * @param formater 格式
    309. * @return
    310. */
    311. public static String getCurrentTime(String formater) {
    312. Date date = new Date();
    313. DateFormat format = new SimpleDateFormat(formater);
    314. return format.format(date);
    315. }
    316. /**
    317. * 计算两经纬度点之间的距离(单位:米)
    318. *
    319. * @param lng1 经度
    320. * @param lat1 纬度
    321. * @param lng2
    322. * @param lat2
    323. * @return getDistance(117.129135, 36.68824, 117.12843556, 36.68851556)
    324. */
    325. public static double getDistance(double lng1, double lat1, double lng2, double lat2) {
    326. double radLat1 = Math.toRadians(lat1);
    327. double radLat2 = Math.toRadians(lat2);
    328. double a = radLat1 - radLat2;
    329. double b = Math.toRadians(lng1) - Math.toRadians(lng2);
    330. double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1)
    331. * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    332. s = s * 6378137.0;// 取WGS84标准参考椭球中的地球长半径(单位:m)
    333. s = Math.round(s * 10000) / 10000;
    334. return s;
    335. }
    336. public static boolean isInteger(String str) {
    337. Pattern pattern = Pattern.compile("^-?[0-9]+");
    338. return pattern.matcher(str).matches();
    339. }
    340. public static long getDistanceTime(String str1, Date date) {
    341. return getDistanceTime(str1, dateToStr(date) );
    342. }
    343. //返回两个时间差 分钟
    344. public static long getDistanceTime(String str1, String str2) {
    345. if (XX.isEmpty(str1) || XX.isEmpty(str2)) {
    346. return 0;
    347. }
    348. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    349. Date one;
    350. Date two;
    351. long min = 0;
    352. try {
    353. one = df.parse(str1);
    354. two = df.parse(str2);
    355. long time1 = one.getTime();
    356. long time2 = two.getTime();
    357. long diff;
    358. if (time1 < time2) {
    359. diff = time2 - time1;
    360. } else {
    361. diff = time1 - time2;
    362. }
    363. min = diff / (60 * 1000);
    364. } catch (Exception e) {
    365. e.printStackTrace();
    366. }
    367. return min;
    368. }
    369. //返回输入日期与当前日期的差 天
    370. public static int getDistanceTime(String str){
    371. if (XX.isEmpty(str)){
    372. return 0;
    373. }
    374. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    375. String currentTimeString = simpleDateFormat.format(new Date());
    376. int deviation=0;
    377. try {
    378. Date currentTime = simpleDateFormat.parse(currentTimeString);
    379. Date parseTime = simpleDateFormat.parse(str);
    380. BigDecimal timeReduction = new BigDecimal(parseTime.getTime() - currentTime.getTime());
    381. BigDecimal timeDivide = timeReduction.divide(new BigDecimal(1000 * 3600 * 24), 0, RoundingMode.UP);
    382. deviation = timeDivide.intValue();
    383. } catch (Exception e) {
    384. e.printStackTrace();
    385. }
    386. return deviation;
    387. }
    388. public static double getStr2Double(String str) {
    389. double ret = 0;
    390. try {
    391. ret = Double.parseDouble(str);
    392. } catch (Exception e) {
    393. }
    394. return ret;
    395. }
    396. //获取xxx分钟之后的时间
    397. public static String getAfterTime(String strTime, int minute) {
    398. String oneMinuteAfterTime = "";
    399. Calendar cal = Calendar.getInstance();
    400. cal.setTime(XX.StrToDate(strTime));
    401. //cal.set(cal.HOUR , cal.HOUR -hour ) ; //把时间设置为当前时间-1小时,同理,也可以设置其他时间
    402. cal.add(cal.MINUTE, minute);
    403. oneMinuteAfterTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime());//获取到完整的时间
    404. return oneMinuteAfterTime;
    405. }
    406. //获取小时之后的时间
    407. public static String getAfterTimeOfHour(String strTime, int hour) {
    408. String afterTimeOfHour = "";
    409. Calendar cal = Calendar.getInstance();
    410. cal.setTime(XX.StrToDate(strTime));
    411. cal.add(Calendar.HOUR, hour);
    412. afterTimeOfHour = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime());//获取到完整的时间
    413. return afterTimeOfHour;
    414. }
    415. //比较时间大小
    416. public static Boolean CompareDate(Date dateOne, Date dateTwo) {
    417. boolean flag = dateOne.getTime() >= dateTwo.getTime();
    418. return flag;
    419. }
    420. public static double getTimeDiff(Date time1, Date time2) {
    421. long t1 = time1.getTime();
    422. long t2 = time2.getTime();
    423. double ret = 0;
    424. if (t1 > t2) {
    425. ret = (t1 - t2) / (60 * 60 * 1000);
    426. } else {
    427. ret = (t2 - t1) / (60 * 60 * 1000);
    428. }
    429. return ret;
    430. }
    431. public static String formatNum(String text, double value) {
    432. DecimalFormat myformat = new DecimalFormat(text);
    433. String output = myformat.format(value);
    434. return output;
    435. }
    436. public static long differentTimesByMillisecond(Date date1, Date date2, int flag) {
    437. long result = 0;
    438. long time1 = date1.getTime();
    439. long time2 = date2.getTime();
    440. switch (flag) {
    441. case 0://毫秒
    442. result = time2 - time1;
    443. break;
    444. case 1://秒
    445. result = (time2 - time1) / 1000;
    446. break;
    447. case 2://分钟
    448. result = (time2 - time1) / (1000 * 60);
    449. break;
    450. case 3://小时
    451. result = (time2 - time1) / (1000 * 60 * 60);
    452. break;
    453. case 4://天
    454. result = (time2 - time1) / (1000 * 3600 * 24);
    455. break;
    456. default:
    457. result = (time2 - time1) / (1000 * 60);
    458. break;
    459. }
    460. return result;
    461. }
    462. /**
    463. * java.time.LocalDateTime --> java.util.Date
    464. * @param localDateTime
    465. * @return
    466. */
    467. public static Date localDateTimeToDate(LocalDateTime localDateTime) {
    468. if (isEmpty(localDateTime)) localDateTime = LocalDateTime.now();
    469. ZoneId zone = ZoneId.systemDefault();
    470. Instant instant = localDateTime.atZone(zone).toInstant();
    471. Date date = Date.from(instant);
    472. return date;
    473. }
    474. /**
    475. * java.util.Date --> java.time.LocalDateTime
    476. * @param date
    477. * @return
    478. */
    479. public static LocalDateTime localDateTimeToDate(Date date) {
    480. Instant instant = Instant.ofEpochMilli(date.getTime());
    481. return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    482. }
    483. /**
    484. * 强转->Double
    485. * @param obj
    486. * @return
    487. */
    488. public static double toDouble(Object obj) {
    489. if(XX.isEmpty(obj)) {
    490. return 0;
    491. }
    492. return Double.parseDouble(obj.toString());
    493. }
    494. /**
    495. * 把map的key转换成驼峰命名
    496. * @param map
    497. * @return
    498. */
    499. public static Map<String, Object> toReplaceKeyLow(Map<String, Object> map) {
    500. Map re_map = new HashMap();
    501. if (re_map != null) {
    502. Iterator var2 = map.entrySet().iterator();
    503. while (var2.hasNext()) {
    504. Map.Entry<String, Object> entry = (Map.Entry) var2.next();
    505. re_map.put(underlineToCamel((String) entry.getKey()), map.get(entry.getKey()));
    506. }
    507. map.clear();
    508. }
    509. return re_map;
    510. }
    511. public static final char UNDERLINE = '_';
    512. public static String underlineToCamel(String param) {
    513. if (param == null || "".equals(param.trim())) {
    514. return "";
    515. }
    516. int len = param.length();
    517. StringBuilder sb = new StringBuilder(len);
    518. for (int i = 0; i < len; i++) {
    519. char c = param.charAt(i);
    520. if (c == UNDERLINE) {
    521. if (++i < len) {
    522. sb.append(Character.toUpperCase(param.charAt(i)));
    523. }
    524. } else {
    525. sb.append(Character.toLowerCase(param.charAt(i)));
    526. }
    527. }
    528. return sb.toString();
    529. }
    530. //比较两个时间大小
    531. //dt1 在dt2前 true
    532. public static boolean compareDate(Date dt1, Date dt2) {
    533. try {
    534. if (dt1.getTime() < dt2.getTime()) {
    535. return true;
    536. } else if (dt1.getTime() > dt2.getTime()) {
    537. return false;
    538. }
    539. } catch (Exception exception) {
    540. exception.printStackTrace();
    541. }
    542. return false;
    543. }
    544. /**
    545. * 封装查询条件,解决字段默认like查询问题
    546. * @param query
    547. * @param column
    548. * @param exp
    549. * @return
    550. */
    551. public static Map<String, Object> getSQLColumn(Map<String, Object> query,String column,String exp) {
    552. if (!isEmpty(query) && !isEmpty(column) && !isEmpty(exp) && !isEmpty(query.get(column))) {
    553. Object value = query.get(column);
    554. query.remove(column);
    555. query.put(column + exp,value);
    556. return query;
    557. }
    558. return new HashMap<String,Object>();
    559. }
    560. /**
    561. * 获取当月的第一天_格式yyyy-MM-dd
    562. * @param datePattern
    563. * @return
    564. */
    565. public static String getFirstMonth(String datePattern){
    566. SimpleDateFormat df=new SimpleDateFormat(datePattern);
    567. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
    568. Date date = new Date();
    569. gc.setTime(date);
    570. gc.set(Calendar.DAY_OF_MONTH, 1);
    571. String day_first = df.format(gc.getTime());
    572. return day_first;
    573. }
    574. /**
    575. * 获取当月的最后一天_格式yyyy-MM-dd
    576. * @param datePattern
    577. * @return
    578. */
    579. public static String getEndMonth(String datePattern){
    580. SimpleDateFormat df=new SimpleDateFormat(datePattern);
    581. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
    582. Date date = new Date();
    583. gc.setTime(date);
    584. gc.add(Calendar.MONTH, 1);
    585. gc.set(Calendar.DAY_OF_MONTH, 0);
    586. String day_end = df.format(gc.getTime());
    587. return day_end;
    588. }
    589. }