作者 RuoYi

删除多余包

1 -package com.ruoyi.common.utils.text;  
2 -  
3 -import java.nio.charset.Charset;  
4 -import java.nio.charset.StandardCharsets;  
5 -import com.ruoyi.common.utils.StringUtils;  
6 -  
7 -/**  
8 - * 字符集工具类  
9 - *  
10 - * @author ruoyi  
11 - *  
12 - */  
13 -public class CharsetKit  
14 -{  
15 - /** ISO-8859-1 */  
16 - public static final String ISO_8859_1 = "ISO-8859-1";  
17 - /** UTF-8 */  
18 - public static final String UTF_8 = "UTF-8";  
19 - /** GBK */  
20 - public static final String GBK = "GBK";  
21 -  
22 - /** ISO-8859-1 */  
23 - public static final Charset CHARSET_ISO_8859_1 = Charset.forName(ISO_8859_1);  
24 - /** UTF-8 */  
25 - public static final Charset CHARSET_UTF_8 = Charset.forName(UTF_8);  
26 - /** GBK */  
27 - public static final Charset CHARSET_GBK = Charset.forName(GBK);  
28 -  
29 - /**  
30 - * 转换为Charset对象  
31 - *  
32 - * @param charset 字符集,为空则返回默认字符集  
33 - * @return Charset  
34 - */  
35 - public static Charset charset(String charset)  
36 - {  
37 - return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset);  
38 - }  
39 -  
40 - /**  
41 - * 转换字符串的字符集编码  
42 - *  
43 - * @param source 字符串  
44 - * @param srcCharset 源字符集,默认ISO-8859-1  
45 - * @param destCharset 目标字符集,默认UTF-8  
46 - * @return 转换后的字符集  
47 - */  
48 - public static String convert(String source, String srcCharset, String destCharset)  
49 - {  
50 - return convert(source, Charset.forName(srcCharset), Charset.forName(destCharset));  
51 - }  
52 -  
53 - /**  
54 - * 转换字符串的字符集编码  
55 - *  
56 - * @param source 字符串  
57 - * @param srcCharset 源字符集,默认ISO-8859-1  
58 - * @param destCharset 目标字符集,默认UTF-8  
59 - * @return 转换后的字符集  
60 - */  
61 - public static String convert(String source, Charset srcCharset, Charset destCharset)  
62 - {  
63 - if (null == srcCharset)  
64 - {  
65 - srcCharset = StandardCharsets.ISO_8859_1;  
66 - }  
67 -  
68 - if (null == destCharset)  
69 - {  
70 - srcCharset = StandardCharsets.UTF_8;  
71 - }  
72 -  
73 - if (StringUtils.isEmpty(source) || srcCharset.equals(destCharset))  
74 - {  
75 - return source;  
76 - }  
77 - return new String(source.getBytes(srcCharset), destCharset);  
78 - }  
79 -  
80 - /**  
81 - * @return 系统字符集编码  
82 - */  
83 - public static String systemCharset()  
84 - {  
85 - return Charset.defaultCharset().name();  
86 - }  
87 -}  
1 -package com.ruoyi.common.utils.text;  
2 -  
3 -import java.math.BigDecimal;  
4 -import java.math.BigInteger;  
5 -import java.nio.ByteBuffer;  
6 -import java.nio.charset.Charset;  
7 -import java.text.NumberFormat;  
8 -import java.util.Set;  
9 -import com.ruoyi.common.utils.StringUtils;  
10 -  
11 -/**  
12 - * 类型转换器  
13 - *  
14 - * @author ruoyi  
15 - */  
16 -public class Convert  
17 -{  
18 - /**  
19 - * 转换为字符串<br>  
20 - * 如果给定的值为null,或者转换失败,返回默认值<br>  
21 - * 转换失败不会报错  
22 - *  
23 - * @param value 被转换的值  
24 - * @param defaultValue 转换错误时的默认值  
25 - * @return 结果  
26 - */  
27 - public static String toStr(Object value, String defaultValue)  
28 - {  
29 - if (null == value)  
30 - {  
31 - return defaultValue;  
32 - }  
33 - if (value instanceof String)  
34 - {  
35 - return (String) value;  
36 - }  
37 - return value.toString();  
38 - }  
39 -  
40 - /**  
41 - * 转换为字符串<br>  
42 - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>  
43 - * 转换失败不会报错  
44 - *  
45 - * @param value 被转换的值  
46 - * @return 结果  
47 - */  
48 - public static String toStr(Object value)  
49 - {  
50 - return toStr(value, null);  
51 - }  
52 -  
53 - /**  
54 - * 转换为字符<br>  
55 - * 如果给定的值为null,或者转换失败,返回默认值<br>  
56 - * 转换失败不会报错  
57 - *  
58 - * @param value 被转换的值  
59 - * @param defaultValue 转换错误时的默认值  
60 - * @return 结果  
61 - */  
62 - public static Character toChar(Object value, Character defaultValue)  
63 - {  
64 - if (null == value)  
65 - {  
66 - return defaultValue;  
67 - }  
68 - if (value instanceof Character)  
69 - {  
70 - return (Character) value;  
71 - }  
72 -  
73 - final String valueStr = toStr(value, null);  
74 - return StringUtils.isEmpty(valueStr) ? defaultValue : valueStr.charAt(0);  
75 - }  
76 -  
77 - /**  
78 - * 转换为字符<br>  
79 - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>  
80 - * 转换失败不会报错  
81 - *  
82 - * @param value 被转换的值  
83 - * @return 结果  
84 - */  
85 - public static Character toChar(Object value)  
86 - {  
87 - return toChar(value, null);  
88 - }  
89 -  
90 - /**  
91 - * 转换为byte<br>  
92 - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<br>  
93 - * 转换失败不会报错  
94 - *  
95 - * @param value 被转换的值  
96 - * @param defaultValue 转换错误时的默认值  
97 - * @return 结果  
98 - */  
99 - public static Byte toByte(Object value, Byte defaultValue)  
100 - {  
101 - if (value == null)  
102 - {  
103 - return defaultValue;  
104 - }  
105 - if (value instanceof Byte)  
106 - {  
107 - return (Byte) value;  
108 - }  
109 - if (value instanceof Number)  
110 - {  
111 - return ((Number) value).byteValue();  
112 - }  
113 - final String valueStr = toStr(value, null);  
114 - if (StringUtils.isEmpty(valueStr))  
115 - {  
116 - return defaultValue;  
117 - }  
118 - try  
119 - {  
120 - return Byte.parseByte(valueStr);  
121 - }  
122 - catch (Exception e)  
123 - {  
124 - return defaultValue;  
125 - }  
126 - }  
127 -  
128 - /**  
129 - * 转换为byte<br>  
130 - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>  
131 - * 转换失败不会报错  
132 - *  
133 - * @param value 被转换的值  
134 - * @return 结果  
135 - */  
136 - public static Byte toByte(Object value)  
137 - {  
138 - return toByte(value, null);  
139 - }  
140 -  
141 - /**  
142 - * 转换为Short<br>  
143 - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<br>  
144 - * 转换失败不会报错  
145 - *  
146 - * @param value 被转换的值  
147 - * @param defaultValue 转换错误时的默认值  
148 - * @return 结果  
149 - */  
150 - public static Short toShort(Object value, Short defaultValue)  
151 - {  
152 - if (value == null)  
153 - {  
154 - return defaultValue;  
155 - }  
156 - if (value instanceof Short)  
157 - {  
158 - return (Short) value;  
159 - }  
160 - if (value instanceof Number)  
161 - {  
162 - return ((Number) value).shortValue();  
163 - }  
164 - final String valueStr = toStr(value, null);  
165 - if (StringUtils.isEmpty(valueStr))  
166 - {  
167 - return defaultValue;  
168 - }  
169 - try  
170 - {  
171 - return Short.parseShort(valueStr.trim());  
172 - }  
173 - catch (Exception e)  
174 - {  
175 - return defaultValue;  
176 - }  
177 - }  
178 -  
179 - /**  
180 - * 转换为Short<br>  
181 - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>  
182 - * 转换失败不会报错  
183 - *  
184 - * @param value 被转换的值  
185 - * @return 结果  
186 - */  
187 - public static Short toShort(Object value)  
188 - {  
189 - return toShort(value, null);  
190 - }  
191 -  
192 - /**  
193 - * 转换为Number<br>  
194 - * 如果给定的值为空,或者转换失败,返回默认值<br>  
195 - * 转换失败不会报错  
196 - *  
197 - * @param value 被转换的值  
198 - * @param defaultValue 转换错误时的默认值  
199 - * @return 结果  
200 - */  
201 - public static Number toNumber(Object value, Number defaultValue)  
202 - {  
203 - if (value == null)  
204 - {  
205 - return defaultValue;  
206 - }  
207 - if (value instanceof Number)  
208 - {  
209 - return (Number) value;  
210 - }  
211 - final String valueStr = toStr(value, null);  
212 - if (StringUtils.isEmpty(valueStr))  
213 - {  
214 - return defaultValue;  
215 - }  
216 - try  
217 - {  
218 - return NumberFormat.getInstance().parse(valueStr);  
219 - }  
220 - catch (Exception e)  
221 - {  
222 - return defaultValue;  
223 - }  
224 - }  
225 -  
226 - /**  
227 - * 转换为Number<br>  
228 - * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>  
229 - * 转换失败不会报错  
230 - *  
231 - * @param value 被转换的值  
232 - * @return 结果  
233 - */  
234 - public static Number toNumber(Object value)  
235 - {  
236 - return toNumber(value, null);  
237 - }  
238 -  
239 - /**  
240 - * 转换为int<br>  
241 - * 如果给定的值为空,或者转换失败,返回默认值<br>  
242 - * 转换失败不会报错  
243 - *  
244 - * @param value 被转换的值  
245 - * @param defaultValue 转换错误时的默认值  
246 - * @return 结果  
247 - */  
248 - public static Integer toInt(Object value, Integer defaultValue)  
249 - {  
250 - if (value == null)  
251 - {  
252 - return defaultValue;  
253 - }  
254 - if (value instanceof Integer)  
255 - {  
256 - return (Integer) value;  
257 - }  
258 - if (value instanceof Number)  
259 - {  
260 - return ((Number) value).intValue();  
261 - }  
262 - final String valueStr = toStr(value, null);  
263 - if (StringUtils.isEmpty(valueStr))  
264 - {  
265 - return defaultValue;  
266 - }  
267 - try  
268 - {  
269 - return Integer.parseInt(valueStr.trim());  
270 - }  
271 - catch (Exception e)  
272 - {  
273 - return defaultValue;  
274 - }  
275 - }  
276 -  
277 - /**  
278 - * 转换为int<br>  
279 - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>  
280 - * 转换失败不会报错  
281 - *  
282 - * @param value 被转换的值  
283 - * @return 结果  
284 - */  
285 - public static Integer toInt(Object value)  
286 - {  
287 - return toInt(value, null);  
288 - }  
289 -  
290 - /**  
291 - * 转换为Integer数组<br>  
292 - *  
293 - * @param str 被转换的值  
294 - * @return 结果  
295 - */  
296 - public static Integer[] toIntArray(String str)  
297 - {  
298 - return toIntArray(",", str);  
299 - }  
300 -  
301 - /**  
302 - * 转换为Long数组<br>  
303 - *  
304 - * @param str 被转换的值  
305 - * @return 结果  
306 - */  
307 - public static Long[] toLongArray(String str)  
308 - {  
309 - return toLongArray(",", str);  
310 - }  
311 -  
312 - /**  
313 - * 转换为Integer数组<br>  
314 - *  
315 - * @param split 分隔符  
316 - * @param split 被转换的值  
317 - * @return 结果  
318 - */  
319 - public static Integer[] toIntArray(String split, String str)  
320 - {  
321 - if (StringUtils.isEmpty(str))  
322 - {  
323 - return new Integer[] {};  
324 - }  
325 - String[] arr = str.split(split);  
326 - final Integer[] ints = new Integer[arr.length];  
327 - for (int i = 0; i < arr.length; i++)  
328 - {  
329 - final Integer v = toInt(arr[i], 0);  
330 - ints[i] = v;  
331 - }  
332 - return ints;  
333 - }  
334 -  
335 - /**  
336 - * 转换为Long数组<br>  
337 - *  
338 - * @param split 分隔符  
339 - * @param str 被转换的值  
340 - * @return 结果  
341 - */  
342 - public static Long[] toLongArray(String split, String str)  
343 - {  
344 - if (StringUtils.isEmpty(str))  
345 - {  
346 - return new Long[] {};  
347 - }  
348 - String[] arr = str.split(split);  
349 - final Long[] longs = new Long[arr.length];  
350 - for (int i = 0; i < arr.length; i++)  
351 - {  
352 - final Long v = toLong(arr[i], null);  
353 - longs[i] = v;  
354 - }  
355 - return longs;  
356 - }  
357 -  
358 - /**  
359 - * 转换为String数组<br>  
360 - *  
361 - * @param str 被转换的值  
362 - * @return 结果  
363 - */  
364 - public static String[] toStrArray(String str)  
365 - {  
366 - return toStrArray(",", str);  
367 - }  
368 -  
369 - /**  
370 - * 转换为String数组<br>  
371 - *  
372 - * @param split 分隔符  
373 - * @param split 被转换的值  
374 - * @return 结果  
375 - */  
376 - public static String[] toStrArray(String split, String str)  
377 - {  
378 - return str.split(split);  
379 - }  
380 -  
381 - /**  
382 - * 转换为long<br>  
383 - * 如果给定的值为空,或者转换失败,返回默认值<br>  
384 - * 转换失败不会报错  
385 - *  
386 - * @param value 被转换的值  
387 - * @param defaultValue 转换错误时的默认值  
388 - * @return 结果  
389 - */  
390 - public static Long toLong(Object value, Long defaultValue)  
391 - {  
392 - if (value == null)  
393 - {  
394 - return defaultValue;  
395 - }  
396 - if (value instanceof Long)  
397 - {  
398 - return (Long) value;  
399 - }  
400 - if (value instanceof Number)  
401 - {  
402 - return ((Number) value).longValue();  
403 - }  
404 - final String valueStr = toStr(value, null);  
405 - if (StringUtils.isEmpty(valueStr))  
406 - {  
407 - return defaultValue;  
408 - }  
409 - try  
410 - {  
411 - // 支持科学计数法  
412 - return new BigDecimal(valueStr.trim()).longValue();  
413 - }  
414 - catch (Exception e)  
415 - {  
416 - return defaultValue;  
417 - }  
418 - }  
419 -  
420 - /**  
421 - * 转换为long<br>  
422 - * 如果给定的值为<code>null</code>,或者转换失败,返回默认值<code>null</code><br>  
423 - * 转换失败不会报错  
424 - *  
425 - * @param value 被转换的值  
426 - * @return 结果  
427 - */  
428 - public static Long toLong(Object value)  
429 - {  
430 - return toLong(value, null);  
431 - }  
432 -  
433 - /**  
434 - * 转换为double<br>  
435 - * 如果给定的值为空,或者转换失败,返回默认值<br>  
436 - * 转换失败不会报错  
437 - *  
438 - * @param value 被转换的值  
439 - * @param defaultValue 转换错误时的默认值  
440 - * @return 结果  
441 - */  
442 - public static Double toDouble(Object value, Double defaultValue)  
443 - {  
444 - if (value == null)  
445 - {  
446 - return defaultValue;  
447 - }  
448 - if (value instanceof Double)  
449 - {  
450 - return (Double) value;  
451 - }  
452 - if (value instanceof Number)  
453 - {  
454 - return ((Number) value).doubleValue();  
455 - }  
456 - final String valueStr = toStr(value, null);  
457 - if (StringUtils.isEmpty(valueStr))  
458 - {  
459 - return defaultValue;  
460 - }  
461 - try  
462 - {  
463 - // 支持科学计数法  
464 - return new BigDecimal(valueStr.trim()).doubleValue();  
465 - }  
466 - catch (Exception e)  
467 - {  
468 - return defaultValue;  
469 - }  
470 - }  
471 -  
472 - /**  
473 - * 转换为double<br>  
474 - * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>  
475 - * 转换失败不会报错  
476 - *  
477 - * @param value 被转换的值  
478 - * @return 结果  
479 - */  
480 - public static Double toDouble(Object value)  
481 - {  
482 - return toDouble(value, null);  
483 - }  
484 -  
485 - /**  
486 - * 转换为Float<br>  
487 - * 如果给定的值为空,或者转换失败,返回默认值<br>  
488 - * 转换失败不会报错  
489 - *  
490 - * @param value 被转换的值  
491 - * @param defaultValue 转换错误时的默认值  
492 - * @return 结果  
493 - */  
494 - public static Float toFloat(Object value, Float defaultValue)  
495 - {  
496 - if (value == null)  
497 - {  
498 - return defaultValue;  
499 - }  
500 - if (value instanceof Float)  
501 - {  
502 - return (Float) value;  
503 - }  
504 - if (value instanceof Number)  
505 - {  
506 - return ((Number) value).floatValue();  
507 - }  
508 - final String valueStr = toStr(value, null);  
509 - if (StringUtils.isEmpty(valueStr))  
510 - {  
511 - return defaultValue;  
512 - }  
513 - try  
514 - {  
515 - return Float.parseFloat(valueStr.trim());  
516 - }  
517 - catch (Exception e)  
518 - {  
519 - return defaultValue;  
520 - }  
521 - }  
522 -  
523 - /**  
524 - * 转换为Float<br>  
525 - * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>  
526 - * 转换失败不会报错  
527 - *  
528 - * @param value 被转换的值  
529 - * @return 结果  
530 - */  
531 - public static Float toFloat(Object value)  
532 - {  
533 - return toFloat(value, null);  
534 - }  
535 -  
536 - /**  
537 - * 转换为boolean<br>  
538 - * String支持的值为:true、false、yes、ok、no,1,0 如果给定的值为空,或者转换失败,返回默认值<br>  
539 - * 转换失败不会报错  
540 - *  
541 - * @param value 被转换的值  
542 - * @param defaultValue 转换错误时的默认值  
543 - * @return 结果  
544 - */  
545 - public static Boolean toBool(Object value, Boolean defaultValue)  
546 - {  
547 - if (value == null)  
548 - {  
549 - return defaultValue;  
550 - }  
551 - if (value instanceof Boolean)  
552 - {  
553 - return (Boolean) value;  
554 - }  
555 - String valueStr = toStr(value, null);  
556 - if (StringUtils.isEmpty(valueStr))  
557 - {  
558 - return defaultValue;  
559 - }  
560 - valueStr = valueStr.trim().toLowerCase();  
561 - switch (valueStr)  
562 - {  
563 - case "true":  
564 - return true;  
565 - case "false":  
566 - return false;  
567 - case "yes":  
568 - return true;  
569 - case "ok":  
570 - return true;  
571 - case "no":  
572 - return false;  
573 - case "1":  
574 - return true;  
575 - case "0":  
576 - return false;  
577 - default:  
578 - return defaultValue;  
579 - }  
580 - }  
581 -  
582 - /**  
583 - * 转换为boolean<br>  
584 - * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>  
585 - * 转换失败不会报错  
586 - *  
587 - * @param value 被转换的值  
588 - * @return 结果  
589 - */  
590 - public static Boolean toBool(Object value)  
591 - {  
592 - return toBool(value, null);  
593 - }  
594 -  
595 - /**  
596 - * 转换为Enum对象<br>  
597 - * 如果给定的值为空,或者转换失败,返回默认值<br>  
598 - *  
599 - * @param clazz Enum的Class  
600 - * @param value 值  
601 - * @param defaultValue 默认值  
602 - * @return Enum  
603 - */  
604 - public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue)  
605 - {  
606 - if (value == null)  
607 - {  
608 - return defaultValue;  
609 - }  
610 - if (clazz.isAssignableFrom(value.getClass()))  
611 - {  
612 - @SuppressWarnings("unchecked")  
613 - E myE = (E) value;  
614 - return myE;  
615 - }  
616 - final String valueStr = toStr(value, null);  
617 - if (StringUtils.isEmpty(valueStr))  
618 - {  
619 - return defaultValue;  
620 - }  
621 - try  
622 - {  
623 - return Enum.valueOf(clazz, valueStr);  
624 - }  
625 - catch (Exception e)  
626 - {  
627 - return defaultValue;  
628 - }  
629 - }  
630 -  
631 - /**  
632 - * 转换为Enum对象<br>  
633 - * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>  
634 - *  
635 - * @param clazz Enum的Class  
636 - * @param value 值  
637 - * @return Enum  
638 - */  
639 - public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value)  
640 - {  
641 - return toEnum(clazz, value, null);  
642 - }  
643 -  
644 - /**  
645 - * 转换为BigInteger<br>  
646 - * 如果给定的值为空,或者转换失败,返回默认值<br>  
647 - * 转换失败不会报错  
648 - *  
649 - * @param value 被转换的值  
650 - * @param defaultValue 转换错误时的默认值  
651 - * @return 结果  
652 - */  
653 - public static BigInteger toBigInteger(Object value, BigInteger defaultValue)  
654 - {  
655 - if (value == null)  
656 - {  
657 - return defaultValue;  
658 - }  
659 - if (value instanceof BigInteger)  
660 - {  
661 - return (BigInteger) value;  
662 - }  
663 - if (value instanceof Long)  
664 - {  
665 - return BigInteger.valueOf((Long) value);  
666 - }  
667 - final String valueStr = toStr(value, null);  
668 - if (StringUtils.isEmpty(valueStr))  
669 - {  
670 - return defaultValue;  
671 - }  
672 - try  
673 - {  
674 - return new BigInteger(valueStr);  
675 - }  
676 - catch (Exception e)  
677 - {  
678 - return defaultValue;  
679 - }  
680 - }  
681 -  
682 - /**  
683 - * 转换为BigInteger<br>  
684 - * 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>  
685 - * 转换失败不会报错  
686 - *  
687 - * @param value 被转换的值  
688 - * @return 结果  
689 - */  
690 - public static BigInteger toBigInteger(Object value)  
691 - {  
692 - return toBigInteger(value, null);  
693 - }  
694 -  
695 - /**  
696 - * 转换为BigDecimal<br>  
697 - * 如果给定的值为空,或者转换失败,返回默认值<br>  
698 - * 转换失败不会报错  
699 - *  
700 - * @param value 被转换的值  
701 - * @param defaultValue 转换错误时的默认值  
702 - * @return 结果  
703 - */  
704 - public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue)  
705 - {  
706 - if (value == null)  
707 - {  
708 - return defaultValue;  
709 - }  
710 - if (value instanceof BigDecimal)  
711 - {  
712 - return (BigDecimal) value;  
713 - }  
714 - if (value instanceof Long)  
715 - {  
716 - return new BigDecimal((Long) value);  
717 - }  
718 - if (value instanceof Double)  
719 - {  
720 - return new BigDecimal((Double) value);  
721 - }  
722 - if (value instanceof Integer)  
723 - {  
724 - return new BigDecimal((Integer) value);  
725 - }  
726 - final String valueStr = toStr(value, null);  
727 - if (StringUtils.isEmpty(valueStr))  
728 - {  
729 - return defaultValue;  
730 - }  
731 - try  
732 - {  
733 - return new BigDecimal(valueStr);  
734 - }  
735 - catch (Exception e)  
736 - {  
737 - return defaultValue;  
738 - }  
739 - }  
740 -  
741 - /**  
742 - * 转换为BigDecimal<br>  
743 - * 如果给定的值为空,或者转换失败,返回默认值<br>  
744 - * 转换失败不会报错  
745 - *  
746 - * @param value 被转换的值  
747 - * @return 结果  
748 - */  
749 - public static BigDecimal toBigDecimal(Object value)  
750 - {  
751 - return toBigDecimal(value, null);  
752 - }  
753 -  
754 - /**  
755 - * 将对象转为字符串<br>  
756 - * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法  
757 - *  
758 - * @param obj 对象  
759 - * @return 字符串  
760 - */  
761 - public static String utf8Str(Object obj)  
762 - {  
763 - return str(obj, CharsetKit.CHARSET_UTF_8);  
764 - }  
765 -  
766 - /**  
767 - * 将对象转为字符串<br>  
768 - * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法  
769 - *  
770 - * @param obj 对象  
771 - * @param charsetName 字符集  
772 - * @return 字符串  
773 - */  
774 - public static String str(Object obj, String charsetName)  
775 - {  
776 - return str(obj, Charset.forName(charsetName));  
777 - }  
778 -  
779 - /**  
780 - * 将对象转为字符串<br>  
781 - * 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法  
782 - *  
783 - * @param obj 对象  
784 - * @param charset 字符集  
785 - * @return 字符串  
786 - */  
787 - public static String str(Object obj, Charset charset)  
788 - {  
789 - if (null == obj)  
790 - {  
791 - return null;  
792 - }  
793 -  
794 - if (obj instanceof String)  
795 - {  
796 - return (String) obj;  
797 - }  
798 - else if (obj instanceof byte[] || obj instanceof Byte[])  
799 - {  
800 - return str((Byte[]) obj, charset);  
801 - }  
802 - else if (obj instanceof ByteBuffer)  
803 - {  
804 - return str((ByteBuffer) obj, charset);  
805 - }  
806 - return obj.toString();  
807 - }  
808 -  
809 - /**  
810 - * 将byte数组转为字符串  
811 - *  
812 - * @param bytes byte数组  
813 - * @param charset 字符集  
814 - * @return 字符串  
815 - */  
816 - public static String str(byte[] bytes, String charset)  
817 - {  
818 - return str(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset));  
819 - }  
820 -  
821 - /**  
822 - * 解码字节码  
823 - *  
824 - * @param data 字符串  
825 - * @param charset 字符集,如果此字段为空,则解码的结果取决于平台  
826 - * @return 解码后的字符串  
827 - */  
828 - public static String str(byte[] data, Charset charset)  
829 - {  
830 - if (data == null)  
831 - {  
832 - return null;  
833 - }  
834 -  
835 - if (null == charset)  
836 - {  
837 - return new String(data);  
838 - }  
839 - return new String(data, charset);  
840 - }  
841 -  
842 - /**  
843 - * 将编码的byteBuffer数据转换为字符串  
844 - *  
845 - * @param data 数据  
846 - * @param charset 字符集,如果为空使用当前系统字符集  
847 - * @return 字符串  
848 - */  
849 - public static String str(ByteBuffer data, String charset)  
850 - {  
851 - if (data == null)  
852 - {  
853 - return null;  
854 - }  
855 -  
856 - return str(data, Charset.forName(charset));  
857 - }  
858 -  
859 - /**  
860 - * 将编码的byteBuffer数据转换为字符串  
861 - *  
862 - * @param data 数据  
863 - * @param charset 字符集,如果为空使用当前系统字符集  
864 - * @return 字符串  
865 - */  
866 - public static String str(ByteBuffer data, Charset charset)  
867 - {  
868 - if (null == charset)  
869 - {  
870 - charset = Charset.defaultCharset();  
871 - }  
872 - return charset.decode(data).toString();  
873 - }  
874 -  
875 - // ----------------------------------------------------------------------- 全角半角转换  
876 - /**  
877 - * 半角转全角  
878 - *  
879 - * @param input String.  
880 - * @return 全角字符串.  
881 - */  
882 - public static String toSBC(String input)  
883 - {  
884 - return toSBC(input, null);  
885 - }  
886 -  
887 - /**  
888 - * 半角转全角  
889 - *  
890 - * @param input String  
891 - * @param notConvertSet 不替换的字符集合  
892 - * @return 全角字符串.  
893 - */  
894 - public static String toSBC(String input, Set<Character> notConvertSet)  
895 - {  
896 - char c[] = input.toCharArray();  
897 - for (int i = 0; i < c.length; i++)  
898 - {  
899 - if (null != notConvertSet && notConvertSet.contains(c[i]))  
900 - {  
901 - // 跳过不替换的字符  
902 - continue;  
903 - }  
904 -  
905 - if (c[i] == ' ')  
906 - {  
907 - c[i] = '\u3000';  
908 - }  
909 - else if (c[i] < '\177')  
910 - {  
911 - c[i] = (char) (c[i] + 65248);  
912 -  
913 - }  
914 - }  
915 - return new String(c);  
916 - }  
917 -  
918 - /**  
919 - * 全角转半角  
920 - *  
921 - * @param input String.  
922 - * @return 半角字符串  
923 - */  
924 - public static String toDBC(String input)  
925 - {  
926 - return toDBC(input, null);  
927 - }  
928 -  
929 - /**  
930 - * 替换全角为半角  
931 - *  
932 - * @param text 文本  
933 - * @param notConvertSet 不替换的字符集合  
934 - * @return 替换后的字符  
935 - */  
936 - public static String toDBC(String text, Set<Character> notConvertSet)  
937 - {  
938 - char c[] = text.toCharArray();  
939 - for (int i = 0; i < c.length; i++)  
940 - {  
941 - if (null != notConvertSet && notConvertSet.contains(c[i]))  
942 - {  
943 - // 跳过不替换的字符  
944 - continue;  
945 - }  
946 -  
947 - if (c[i] == '\u3000')  
948 - {  
949 - c[i] = ' ';  
950 - }  
951 - else if (c[i] > '\uFF00' && c[i] < '\uFF5F')  
952 - {  
953 - c[i] = (char) (c[i] - 65248);  
954 - }  
955 - }  
956 - String returnString = new String(c);  
957 -  
958 - return returnString;  
959 - }  
960 -  
961 - /**  
962 - * 数字金额大写转换 先写个完整的然后将如零拾替换成零  
963 - *  
964 - * @param n 数字  
965 - * @return 中文大写数字  
966 - */  
967 - public static String digitUppercase(double n)  
968 - {  
969 - String[] fraction = { "角", "分" };  
970 - String[] digit = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };  
971 - String[][] unit = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } };  
972 -  
973 - String head = n < 0 ? "负" : "";  
974 - n = Math.abs(n);  
975 -  
976 - String s = "";  
977 - for (int i = 0; i < fraction.length; i++)  
978 - {  
979 - s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");  
980 - }  
981 - if (s.length() < 1)  
982 - {  
983 - s = "整";  
984 - }  
985 - int integerPart = (int) Math.floor(n);  
986 -  
987 - for (int i = 0; i < unit[0].length && integerPart > 0; i++)  
988 - {  
989 - String p = "";  
990 - for (int j = 0; j < unit[1].length && n > 0; j++)  
991 - {  
992 - p = digit[integerPart % 10] + unit[1][j] + p;  
993 - integerPart = integerPart / 10;  
994 - }  
995 - s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;  
996 - }  
997 - return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");  
998 - }  
999 -}  
1 -package com.ruoyi.common.utils.text;  
2 -  
3 -import com.ruoyi.common.utils.StringUtils;  
4 -  
5 -/**  
6 - * 字符串格式化  
7 - *  
8 - * @author ruoyi  
9 - */  
10 -public class StrFormatter  
11 -{  
12 - public static final String EMPTY_JSON = "{}";  
13 - public static final char C_BACKSLASH = '\\';  
14 - public static final char C_DELIM_START = '{';  
15 - public static final char C_DELIM_END = '}';  
16 -  
17 - /**  
18 - * 格式化字符串<br>  
19 - * 此方法只是简单将占位符 {} 按照顺序替换为参数<br>  
20 - * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>  
21 - * 例:<br>  
22 - * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>  
23 - * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>  
24 - * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>  
25 - *  
26 - * @param strPattern 字符串模板  
27 - * @param argArray 参数列表  
28 - * @return 结果  
29 - */  
30 - public static String format(final String strPattern, final Object... argArray)  
31 - {  
32 - if (StringUtils.isEmpty(strPattern) || StringUtils.isEmpty(argArray))  
33 - {  
34 - return strPattern;  
35 - }  
36 - final int strPatternLength = strPattern.length();  
37 -  
38 - // 初始化定义好的长度以获得更好的性能  
39 - StringBuilder sbuf = new StringBuilder(strPatternLength + 50);  
40 -  
41 - int handledPosition = 0;  
42 - int delimIndex;// 占位符所在位置  
43 - for (int argIndex = 0; argIndex < argArray.length; argIndex++)  
44 - {  
45 - delimIndex = strPattern.indexOf(EMPTY_JSON, handledPosition);  
46 - if (delimIndex == -1)  
47 - {  
48 - if (handledPosition == 0)  
49 - {  
50 - return strPattern;  
51 - }  
52 - else  
53 - { // 字符串模板剩余部分不再包含占位符,加入剩余部分后返回结果  
54 - sbuf.append(strPattern, handledPosition, strPatternLength);  
55 - return sbuf.toString();  
56 - }  
57 - }  
58 - else  
59 - {  
60 - if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == C_BACKSLASH)  
61 - {  
62 - if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == C_BACKSLASH)  
63 - {  
64 - // 转义符之前还有一个转义符,占位符依旧有效  
65 - sbuf.append(strPattern, handledPosition, delimIndex - 1);  
66 - sbuf.append(Convert.utf8Str(argArray[argIndex]));  
67 - handledPosition = delimIndex + 2;  
68 - }  
69 - else  
70 - {  
71 - // 占位符被转义  
72 - argIndex--;  
73 - sbuf.append(strPattern, handledPosition, delimIndex - 1);  
74 - sbuf.append(C_DELIM_START);  
75 - handledPosition = delimIndex + 1;  
76 - }  
77 - }  
78 - else  
79 - {  
80 - // 正常占位符  
81 - sbuf.append(strPattern, handledPosition, delimIndex);  
82 - sbuf.append(Convert.utf8Str(argArray[argIndex]));  
83 - handledPosition = delimIndex + 2;  
84 - }  
85 - }  
86 - }  
87 - // append the characters following the last {} pair.  
88 - // 加入最后一个占位符后所有的字符  
89 - sbuf.append(strPattern, handledPosition, strPattern.length());  
90 -  
91 - return sbuf.toString();  
92 - }  
93 -}