|
@@ -6,16 +6,19 @@ import java.nio.ByteBuffer;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.text.NumberFormat;
|
|
|
import java.util.Set;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
import com.zkqy.common.utils.StringUtils;
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
+import net.sourceforge.pinyin4j.PinyinHelper;
|
|
|
|
|
|
/**
|
|
|
* 类型转换器
|
|
|
*
|
|
|
* @author zkqy
|
|
|
*/
|
|
|
-public class Convert
|
|
|
-{
|
|
|
+public class Convert {
|
|
|
/**
|
|
|
* 转换为字符串<br>
|
|
|
* 如果给定的值为null,或者转换失败,返回默认值<br>
|
|
@@ -25,15 +28,12 @@ public class Convert
|
|
|
* @param defaultValue 转换错误时的默认值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static String toStr(Object value, String defaultValue)
|
|
|
- {
|
|
|
- if (null == value)
|
|
|
- {
|
|
|
+ public static String toStr(Object value, String defaultValue) {
|
|
|
+ if (null == value) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (value instanceof String)
|
|
|
- {
|
|
|
- return (String) value;
|
|
|
+ if (value instanceof String) {
|
|
|
+ return (String)value;
|
|
|
}
|
|
|
return value.toString();
|
|
|
}
|
|
@@ -46,8 +46,7 @@ public class Convert
|
|
|
* @param value 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static String toStr(Object value)
|
|
|
- {
|
|
|
+ public static String toStr(Object value) {
|
|
|
return toStr(value, null);
|
|
|
}
|
|
|
|
|
@@ -60,15 +59,12 @@ public class Convert
|
|
|
* @param defaultValue 转换错误时的默认值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Character toChar(Object value, Character defaultValue)
|
|
|
- {
|
|
|
- if (null == value)
|
|
|
- {
|
|
|
+ public static Character toChar(Object value, Character defaultValue) {
|
|
|
+ if (null == value) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (value instanceof Character)
|
|
|
- {
|
|
|
- return (Character) value;
|
|
|
+ if (value instanceof Character) {
|
|
|
+ return (Character)value;
|
|
|
}
|
|
|
|
|
|
final String valueStr = toStr(value, null);
|
|
@@ -83,8 +79,7 @@ public class Convert
|
|
|
* @param value 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Character toChar(Object value)
|
|
|
- {
|
|
|
+ public static Character toChar(Object value) {
|
|
|
return toChar(value, null);
|
|
|
}
|
|
|
|
|
@@ -97,31 +92,23 @@ public class Convert
|
|
|
* @param defaultValue 转换错误时的默认值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Byte toByte(Object value, Byte defaultValue)
|
|
|
- {
|
|
|
- if (value == null)
|
|
|
- {
|
|
|
+ public static Byte toByte(Object value, Byte defaultValue) {
|
|
|
+ if (value == null) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (value instanceof Byte)
|
|
|
- {
|
|
|
- return (Byte) value;
|
|
|
+ if (value instanceof Byte) {
|
|
|
+ return (Byte)value;
|
|
|
}
|
|
|
- if (value instanceof Number)
|
|
|
- {
|
|
|
- return ((Number) value).byteValue();
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number)value).byteValue();
|
|
|
}
|
|
|
final String valueStr = toStr(value, null);
|
|
|
- if (StringUtils.isEmpty(valueStr))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
return Byte.parseByte(valueStr);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
}
|
|
@@ -134,8 +121,7 @@ public class Convert
|
|
|
* @param value 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Byte toByte(Object value)
|
|
|
- {
|
|
|
+ public static Byte toByte(Object value) {
|
|
|
return toByte(value, null);
|
|
|
}
|
|
|
|
|
@@ -148,31 +134,23 @@ public class Convert
|
|
|
* @param defaultValue 转换错误时的默认值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Short toShort(Object value, Short defaultValue)
|
|
|
- {
|
|
|
- if (value == null)
|
|
|
- {
|
|
|
+ public static Short toShort(Object value, Short defaultValue) {
|
|
|
+ if (value == null) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (value instanceof Short)
|
|
|
- {
|
|
|
- return (Short) value;
|
|
|
+ if (value instanceof Short) {
|
|
|
+ return (Short)value;
|
|
|
}
|
|
|
- if (value instanceof Number)
|
|
|
- {
|
|
|
- return ((Number) value).shortValue();
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number)value).shortValue();
|
|
|
}
|
|
|
final String valueStr = toStr(value, null);
|
|
|
- if (StringUtils.isEmpty(valueStr))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
return Short.parseShort(valueStr.trim());
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
}
|
|
@@ -185,8 +163,7 @@ public class Convert
|
|
|
* @param value 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Short toShort(Object value)
|
|
|
- {
|
|
|
+ public static Short toShort(Object value) {
|
|
|
return toShort(value, null);
|
|
|
}
|
|
|
|
|
@@ -199,27 +176,20 @@ public class Convert
|
|
|
* @param defaultValue 转换错误时的默认值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Number toNumber(Object value, Number defaultValue)
|
|
|
- {
|
|
|
- if (value == null)
|
|
|
- {
|
|
|
+ public static Number toNumber(Object value, Number defaultValue) {
|
|
|
+ if (value == null) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (value instanceof Number)
|
|
|
- {
|
|
|
- return (Number) value;
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return (Number)value;
|
|
|
}
|
|
|
final String valueStr = toStr(value, null);
|
|
|
- if (StringUtils.isEmpty(valueStr))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
return NumberFormat.getInstance().parse(valueStr);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
}
|
|
@@ -232,8 +202,7 @@ public class Convert
|
|
|
* @param value 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Number toNumber(Object value)
|
|
|
- {
|
|
|
+ public static Number toNumber(Object value) {
|
|
|
return toNumber(value, null);
|
|
|
}
|
|
|
|
|
@@ -246,31 +215,23 @@ public class Convert
|
|
|
* @param defaultValue 转换错误时的默认值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Integer toInt(Object value, Integer defaultValue)
|
|
|
- {
|
|
|
- if (value == null)
|
|
|
- {
|
|
|
+ public static Integer toInt(Object value, Integer defaultValue) {
|
|
|
+ if (value == null) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (value instanceof Integer)
|
|
|
- {
|
|
|
- return (Integer) value;
|
|
|
+ if (value instanceof Integer) {
|
|
|
+ return (Integer)value;
|
|
|
}
|
|
|
- if (value instanceof Number)
|
|
|
- {
|
|
|
- return ((Number) value).intValue();
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number)value).intValue();
|
|
|
}
|
|
|
final String valueStr = toStr(value, null);
|
|
|
- if (StringUtils.isEmpty(valueStr))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
return Integer.parseInt(valueStr.trim());
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
}
|
|
@@ -283,8 +244,7 @@ public class Convert
|
|
|
* @param value 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Integer toInt(Object value)
|
|
|
- {
|
|
|
+ public static Integer toInt(Object value) {
|
|
|
return toInt(value, null);
|
|
|
}
|
|
|
|
|
@@ -294,8 +254,7 @@ public class Convert
|
|
|
* @param str 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Integer[] toIntArray(String str)
|
|
|
- {
|
|
|
+ public static Integer[] toIntArray(String str) {
|
|
|
return toIntArray(",", str);
|
|
|
}
|
|
|
|
|
@@ -305,8 +264,7 @@ public class Convert
|
|
|
* @param str 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Long[] toLongArray(String str)
|
|
|
- {
|
|
|
+ public static Long[] toLongArray(String str) {
|
|
|
return toLongArray(",", str);
|
|
|
}
|
|
|
|
|
@@ -317,16 +275,13 @@ public class Convert
|
|
|
* @param split 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Integer[] toIntArray(String split, String str)
|
|
|
- {
|
|
|
- if (StringUtils.isEmpty(str))
|
|
|
- {
|
|
|
+ public static Integer[] toIntArray(String split, String str) {
|
|
|
+ if (StringUtils.isEmpty(str)) {
|
|
|
return new Integer[] {};
|
|
|
}
|
|
|
String[] arr = str.split(split);
|
|
|
final Integer[] ints = new Integer[arr.length];
|
|
|
- for (int i = 0; i < arr.length; i++)
|
|
|
- {
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
final Integer v = toInt(arr[i], 0);
|
|
|
ints[i] = v;
|
|
|
}
|
|
@@ -340,16 +295,13 @@ public class Convert
|
|
|
* @param str 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Long[] toLongArray(String split, String str)
|
|
|
- {
|
|
|
- if (StringUtils.isEmpty(str))
|
|
|
- {
|
|
|
+ public static Long[] toLongArray(String split, String str) {
|
|
|
+ if (StringUtils.isEmpty(str)) {
|
|
|
return new Long[] {};
|
|
|
}
|
|
|
String[] arr = str.split(split);
|
|
|
final Long[] longs = new Long[arr.length];
|
|
|
- for (int i = 0; i < arr.length; i++)
|
|
|
- {
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
final Long v = toLong(arr[i], null);
|
|
|
longs[i] = v;
|
|
|
}
|
|
@@ -362,8 +314,7 @@ public class Convert
|
|
|
* @param str 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static String[] toStrArray(String str)
|
|
|
- {
|
|
|
+ public static String[] toStrArray(String str) {
|
|
|
return toStrArray(",", str);
|
|
|
}
|
|
|
|
|
@@ -374,8 +325,7 @@ public class Convert
|
|
|
* @param split 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static String[] toStrArray(String split, String str)
|
|
|
- {
|
|
|
+ public static String[] toStrArray(String split, String str) {
|
|
|
return str.split(split);
|
|
|
}
|
|
|
|
|
@@ -388,32 +338,24 @@ public class Convert
|
|
|
* @param defaultValue 转换错误时的默认值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Long toLong(Object value, Long defaultValue)
|
|
|
- {
|
|
|
- if (value == null)
|
|
|
- {
|
|
|
+ public static Long toLong(Object value, Long defaultValue) {
|
|
|
+ if (value == null) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (value instanceof Long)
|
|
|
- {
|
|
|
- return (Long) value;
|
|
|
+ if (value instanceof Long) {
|
|
|
+ return (Long)value;
|
|
|
}
|
|
|
- if (value instanceof Number)
|
|
|
- {
|
|
|
- return ((Number) value).longValue();
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number)value).longValue();
|
|
|
}
|
|
|
final String valueStr = toStr(value, null);
|
|
|
- if (StringUtils.isEmpty(valueStr))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
// 支持科学计数法
|
|
|
return new BigDecimal(valueStr.trim()).longValue();
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
}
|
|
@@ -426,8 +368,7 @@ public class Convert
|
|
|
* @param value 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Long toLong(Object value)
|
|
|
- {
|
|
|
+ public static Long toLong(Object value) {
|
|
|
return toLong(value, null);
|
|
|
}
|
|
|
|
|
@@ -440,32 +381,24 @@ public class Convert
|
|
|
* @param defaultValue 转换错误时的默认值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Double toDouble(Object value, Double defaultValue)
|
|
|
- {
|
|
|
- if (value == null)
|
|
|
- {
|
|
|
+ public static Double toDouble(Object value, Double defaultValue) {
|
|
|
+ if (value == null) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (value instanceof Double)
|
|
|
- {
|
|
|
- return (Double) value;
|
|
|
+ if (value instanceof Double) {
|
|
|
+ return (Double)value;
|
|
|
}
|
|
|
- if (value instanceof Number)
|
|
|
- {
|
|
|
- return ((Number) value).doubleValue();
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number)value).doubleValue();
|
|
|
}
|
|
|
final String valueStr = toStr(value, null);
|
|
|
- if (StringUtils.isEmpty(valueStr))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
// 支持科学计数法
|
|
|
return new BigDecimal(valueStr.trim()).doubleValue();
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
}
|
|
@@ -478,8 +411,7 @@ public class Convert
|
|
|
* @param value 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Double toDouble(Object value)
|
|
|
- {
|
|
|
+ public static Double toDouble(Object value) {
|
|
|
return toDouble(value, null);
|
|
|
}
|
|
|
|
|
@@ -492,31 +424,23 @@ public class Convert
|
|
|
* @param defaultValue 转换错误时的默认值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Float toFloat(Object value, Float defaultValue)
|
|
|
- {
|
|
|
- if (value == null)
|
|
|
- {
|
|
|
+ public static Float toFloat(Object value, Float defaultValue) {
|
|
|
+ if (value == null) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (value instanceof Float)
|
|
|
- {
|
|
|
- return (Float) value;
|
|
|
+ if (value instanceof Float) {
|
|
|
+ return (Float)value;
|
|
|
}
|
|
|
- if (value instanceof Number)
|
|
|
- {
|
|
|
- return ((Number) value).floatValue();
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number)value).floatValue();
|
|
|
}
|
|
|
final String valueStr = toStr(value, null);
|
|
|
- if (StringUtils.isEmpty(valueStr))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
return Float.parseFloat(valueStr.trim());
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
}
|
|
@@ -529,8 +453,7 @@ public class Convert
|
|
|
* @param value 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Float toFloat(Object value)
|
|
|
- {
|
|
|
+ public static Float toFloat(Object value) {
|
|
|
return toFloat(value, null);
|
|
|
}
|
|
|
|
|
@@ -543,24 +466,19 @@ public class Convert
|
|
|
* @param defaultValue 转换错误时的默认值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Boolean toBool(Object value, Boolean defaultValue)
|
|
|
- {
|
|
|
- if (value == null)
|
|
|
- {
|
|
|
+ public static Boolean toBool(Object value, Boolean defaultValue) {
|
|
|
+ if (value == null) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (value instanceof Boolean)
|
|
|
- {
|
|
|
- return (Boolean) value;
|
|
|
+ if (value instanceof Boolean) {
|
|
|
+ return (Boolean)value;
|
|
|
}
|
|
|
String valueStr = toStr(value, null);
|
|
|
- if (StringUtils.isEmpty(valueStr))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
valueStr = valueStr.trim().toLowerCase();
|
|
|
- switch (valueStr)
|
|
|
- {
|
|
|
+ switch (valueStr) {
|
|
|
case "true":
|
|
|
case "yes":
|
|
|
case "ok":
|
|
@@ -583,8 +501,7 @@ public class Convert
|
|
|
* @param value 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static Boolean toBool(Object value)
|
|
|
- {
|
|
|
+ public static Boolean toBool(Object value) {
|
|
|
return toBool(value, null);
|
|
|
}
|
|
|
|
|
@@ -597,29 +514,22 @@ public class Convert
|
|
|
* @param defaultValue 默认值
|
|
|
* @return Enum
|
|
|
*/
|
|
|
- public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue)
|
|
|
- {
|
|
|
- if (value == null)
|
|
|
- {
|
|
|
+ public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue) {
|
|
|
+ if (value == null) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (clazz.isAssignableFrom(value.getClass()))
|
|
|
- {
|
|
|
+ if (clazz.isAssignableFrom(value.getClass())) {
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- E myE = (E) value;
|
|
|
+ E myE = (E)value;
|
|
|
return myE;
|
|
|
}
|
|
|
final String valueStr = toStr(value, null);
|
|
|
- if (StringUtils.isEmpty(valueStr))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
return Enum.valueOf(clazz, valueStr);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
}
|
|
@@ -632,8 +542,7 @@ public class Convert
|
|
|
* @param value 值
|
|
|
* @return Enum
|
|
|
*/
|
|
|
- public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value)
|
|
|
- {
|
|
|
+ public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value) {
|
|
|
return toEnum(clazz, value, null);
|
|
|
}
|
|
|
|
|
@@ -646,31 +555,23 @@ public class Convert
|
|
|
* @param defaultValue 转换错误时的默认值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static BigInteger toBigInteger(Object value, BigInteger defaultValue)
|
|
|
- {
|
|
|
- if (value == null)
|
|
|
- {
|
|
|
+ public static BigInteger toBigInteger(Object value, BigInteger defaultValue) {
|
|
|
+ if (value == null) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (value instanceof BigInteger)
|
|
|
- {
|
|
|
- return (BigInteger) value;
|
|
|
+ if (value instanceof BigInteger) {
|
|
|
+ return (BigInteger)value;
|
|
|
}
|
|
|
- if (value instanceof Long)
|
|
|
- {
|
|
|
- return BigInteger.valueOf((Long) value);
|
|
|
+ if (value instanceof Long) {
|
|
|
+ return BigInteger.valueOf((Long)value);
|
|
|
}
|
|
|
final String valueStr = toStr(value, null);
|
|
|
- if (StringUtils.isEmpty(valueStr))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
return new BigInteger(valueStr);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
}
|
|
@@ -683,8 +584,7 @@ public class Convert
|
|
|
* @param value 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static BigInteger toBigInteger(Object value)
|
|
|
- {
|
|
|
+ public static BigInteger toBigInteger(Object value) {
|
|
|
return toBigInteger(value, null);
|
|
|
}
|
|
|
|
|
@@ -697,39 +597,29 @@ public class Convert
|
|
|
* @param defaultValue 转换错误时的默认值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue)
|
|
|
- {
|
|
|
- if (value == null)
|
|
|
- {
|
|
|
+ public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) {
|
|
|
+ if (value == null) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- if (value instanceof BigDecimal)
|
|
|
- {
|
|
|
- return (BigDecimal) value;
|
|
|
+ if (value instanceof BigDecimal) {
|
|
|
+ return (BigDecimal)value;
|
|
|
}
|
|
|
- if (value instanceof Long)
|
|
|
- {
|
|
|
- return new BigDecimal((Long) value);
|
|
|
+ if (value instanceof Long) {
|
|
|
+ return new BigDecimal((Long)value);
|
|
|
}
|
|
|
- if (value instanceof Double)
|
|
|
- {
|
|
|
- return BigDecimal.valueOf((Double) value);
|
|
|
+ if (value instanceof Double) {
|
|
|
+ return BigDecimal.valueOf((Double)value);
|
|
|
}
|
|
|
- if (value instanceof Integer)
|
|
|
- {
|
|
|
- return new BigDecimal((Integer) value);
|
|
|
+ if (value instanceof Integer) {
|
|
|
+ return new BigDecimal((Integer)value);
|
|
|
}
|
|
|
final String valueStr = toStr(value, null);
|
|
|
- if (StringUtils.isEmpty(valueStr))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(valueStr)) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
return new BigDecimal(valueStr);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
return defaultValue;
|
|
|
}
|
|
|
}
|
|
@@ -742,8 +632,7 @@ public class Convert
|
|
|
* @param value 被转换的值
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public static BigDecimal toBigDecimal(Object value)
|
|
|
- {
|
|
|
+ public static BigDecimal toBigDecimal(Object value) {
|
|
|
return toBigDecimal(value, null);
|
|
|
}
|
|
|
|
|
@@ -754,8 +643,7 @@ public class Convert
|
|
|
* @param obj 对象
|
|
|
* @return 字符串
|
|
|
*/
|
|
|
- public static String utf8Str(Object obj)
|
|
|
- {
|
|
|
+ public static String utf8Str(Object obj) {
|
|
|
return str(obj, CharsetKit.CHARSET_UTF_8);
|
|
|
}
|
|
|
|
|
@@ -767,8 +655,7 @@ public class Convert
|
|
|
* @param charsetName 字符集
|
|
|
* @return 字符串
|
|
|
*/
|
|
|
- public static String str(Object obj, String charsetName)
|
|
|
- {
|
|
|
+ public static String str(Object obj, String charsetName) {
|
|
|
return str(obj, Charset.forName(charsetName));
|
|
|
}
|
|
|
|
|
@@ -780,29 +667,20 @@ public class Convert
|
|
|
* @param charset 字符集
|
|
|
* @return 字符串
|
|
|
*/
|
|
|
- public static String str(Object obj, Charset charset)
|
|
|
- {
|
|
|
- if (null == obj)
|
|
|
- {
|
|
|
+ public static String str(Object obj, Charset charset) {
|
|
|
+ if (null == obj) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- if (obj instanceof String)
|
|
|
- {
|
|
|
- return (String) obj;
|
|
|
- }
|
|
|
- else if (obj instanceof byte[])
|
|
|
- {
|
|
|
- return str((byte[]) obj, charset);
|
|
|
- }
|
|
|
- else if (obj instanceof Byte[])
|
|
|
- {
|
|
|
- byte[] bytes = ArrayUtils.toPrimitive((Byte[]) obj);
|
|
|
+ if (obj instanceof String) {
|
|
|
+ return (String)obj;
|
|
|
+ } else if (obj instanceof byte[]) {
|
|
|
+ return str((byte[])obj, charset);
|
|
|
+ } else if (obj instanceof Byte[]) {
|
|
|
+ byte[] bytes = ArrayUtils.toPrimitive((Byte[])obj);
|
|
|
return str(bytes, charset);
|
|
|
- }
|
|
|
- else if (obj instanceof ByteBuffer)
|
|
|
- {
|
|
|
- return str((ByteBuffer) obj, charset);
|
|
|
+ } else if (obj instanceof ByteBuffer) {
|
|
|
+ return str((ByteBuffer)obj, charset);
|
|
|
}
|
|
|
return obj.toString();
|
|
|
}
|
|
@@ -814,8 +692,7 @@ public class Convert
|
|
|
* @param charset 字符集
|
|
|
* @return 字符串
|
|
|
*/
|
|
|
- public static String str(byte[] bytes, String charset)
|
|
|
- {
|
|
|
+ public static String str(byte[] bytes, String charset) {
|
|
|
return str(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset));
|
|
|
}
|
|
|
|
|
@@ -826,15 +703,12 @@ public class Convert
|
|
|
* @param charset 字符集,如果此字段为空,则解码的结果取决于平台
|
|
|
* @return 解码后的字符串
|
|
|
*/
|
|
|
- public static String str(byte[] data, Charset charset)
|
|
|
- {
|
|
|
- if (data == null)
|
|
|
- {
|
|
|
+ public static String str(byte[] data, Charset charset) {
|
|
|
+ if (data == null) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- if (null == charset)
|
|
|
- {
|
|
|
+ if (null == charset) {
|
|
|
return new String(data);
|
|
|
}
|
|
|
return new String(data, charset);
|
|
@@ -847,10 +721,8 @@ public class Convert
|
|
|
* @param charset 字符集,如果为空使用当前系统字符集
|
|
|
* @return 字符串
|
|
|
*/
|
|
|
- public static String str(ByteBuffer data, String charset)
|
|
|
- {
|
|
|
- if (data == null)
|
|
|
- {
|
|
|
+ public static String str(ByteBuffer data, String charset) {
|
|
|
+ if (data == null) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
@@ -864,10 +736,8 @@ public class Convert
|
|
|
* @param charset 字符集,如果为空使用当前系统字符集
|
|
|
* @return 字符串
|
|
|
*/
|
|
|
- public static String str(ByteBuffer data, Charset charset)
|
|
|
- {
|
|
|
- if (null == charset)
|
|
|
- {
|
|
|
+ public static String str(ByteBuffer data, Charset charset) {
|
|
|
+ if (null == charset) {
|
|
|
charset = Charset.defaultCharset();
|
|
|
}
|
|
|
return charset.decode(data).toString();
|
|
@@ -880,8 +750,7 @@ public class Convert
|
|
|
* @param input String.
|
|
|
* @return 全角字符串.
|
|
|
*/
|
|
|
- public static String toSBC(String input)
|
|
|
- {
|
|
|
+ public static String toSBC(String input) {
|
|
|
return toSBC(input, null);
|
|
|
}
|
|
|
|
|
@@ -892,24 +761,18 @@ public class Convert
|
|
|
* @param notConvertSet 不替换的字符集合
|
|
|
* @return 全角字符串.
|
|
|
*/
|
|
|
- public static String toSBC(String input, Set<Character> notConvertSet)
|
|
|
- {
|
|
|
+ public static String toSBC(String input, Set<Character> notConvertSet) {
|
|
|
char[] c = input.toCharArray();
|
|
|
- for (int i = 0; i < c.length; i++)
|
|
|
- {
|
|
|
- if (null != notConvertSet && notConvertSet.contains(c[i]))
|
|
|
- {
|
|
|
+ for (int i = 0; i < c.length; i++) {
|
|
|
+ if (null != notConvertSet && notConvertSet.contains(c[i])) {
|
|
|
// 跳过不替换的字符
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- if (c[i] == ' ')
|
|
|
- {
|
|
|
+ if (c[i] == ' ') {
|
|
|
c[i] = '\u3000';
|
|
|
- }
|
|
|
- else if (c[i] < '\177')
|
|
|
- {
|
|
|
- c[i] = (char) (c[i] + 65248);
|
|
|
+ } else if (c[i] < '\177') {
|
|
|
+ c[i] = (char)(c[i] + 65248);
|
|
|
|
|
|
}
|
|
|
}
|
|
@@ -922,8 +785,7 @@ public class Convert
|
|
|
* @param input String.
|
|
|
* @return 半角字符串
|
|
|
*/
|
|
|
- public static String toDBC(String input)
|
|
|
- {
|
|
|
+ public static String toDBC(String input) {
|
|
|
return toDBC(input, null);
|
|
|
}
|
|
|
|
|
@@ -934,24 +796,18 @@ public class Convert
|
|
|
* @param notConvertSet 不替换的字符集合
|
|
|
* @return 替换后的字符
|
|
|
*/
|
|
|
- public static String toDBC(String text, Set<Character> notConvertSet)
|
|
|
- {
|
|
|
+ public static String toDBC(String text, Set<Character> notConvertSet) {
|
|
|
char[] c = text.toCharArray();
|
|
|
- for (int i = 0; i < c.length; i++)
|
|
|
- {
|
|
|
- if (null != notConvertSet && notConvertSet.contains(c[i]))
|
|
|
- {
|
|
|
+ for (int i = 0; i < c.length; i++) {
|
|
|
+ if (null != notConvertSet && notConvertSet.contains(c[i])) {
|
|
|
// 跳过不替换的字符
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- if (c[i] == '\u3000')
|
|
|
- {
|
|
|
+ if (c[i] == '\u3000') {
|
|
|
c[i] = ' ';
|
|
|
- }
|
|
|
- else if (c[i] > '\uFF00' && c[i] < '\uFF5F')
|
|
|
- {
|
|
|
- c[i] = (char) (c[i] - 65248);
|
|
|
+ } else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
|
|
|
+ c[i] = (char)(c[i] - 65248);
|
|
|
}
|
|
|
}
|
|
|
String returnString = new String(c);
|
|
@@ -965,36 +821,77 @@ public class Convert
|
|
|
* @param n 数字
|
|
|
* @return 中文大写数字
|
|
|
*/
|
|
|
- public static String digitUppercase(double n)
|
|
|
- {
|
|
|
- String[] fraction = { "角", "分" };
|
|
|
- String[] digit = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
|
|
|
- String[][] unit = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } };
|
|
|
+ public static String digitUppercase(double n) {
|
|
|
+ String[] fraction = {"角", "分"};
|
|
|
+ String[] digit = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
|
|
|
+ String[][] unit = {{"元", "万", "亿"}, {"", "拾", "佰", "仟"}};
|
|
|
|
|
|
String head = n < 0 ? "负" : "";
|
|
|
n = Math.abs(n);
|
|
|
|
|
|
String s = "";
|
|
|
- for (int i = 0; i < fraction.length; i++)
|
|
|
- {
|
|
|
- s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
|
|
|
+ for (int i = 0; i < fraction.length; i++) {
|
|
|
+ s += (digit[(int)(Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
|
|
|
}
|
|
|
- if (s.length() < 1)
|
|
|
- {
|
|
|
+ if (s.length() < 1) {
|
|
|
s = "整";
|
|
|
}
|
|
|
- int integerPart = (int) Math.floor(n);
|
|
|
+ int integerPart = (int)Math.floor(n);
|
|
|
|
|
|
- for (int i = 0; i < unit[0].length && integerPart > 0; i++)
|
|
|
- {
|
|
|
+ for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
|
|
|
String p = "";
|
|
|
- for (int j = 0; j < unit[1].length && n > 0; j++)
|
|
|
- {
|
|
|
+ for (int j = 0; j < unit[1].length && n > 0; j++) {
|
|
|
p = digit[integerPart % 10] + unit[1][j] + p;
|
|
|
integerPart = integerPart / 10;
|
|
|
}
|
|
|
s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
|
|
|
}
|
|
|
- return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
|
|
|
+ return head
|
|
|
+ + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 中文汉字转换pinyin
|
|
|
+ *
|
|
|
+ * @param chineseWithLabels 需要转换的参数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String toInitials(String chineseWithLabels) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ // 使用正则表达式匹配汉字
|
|
|
+ Pattern pattern = Pattern.compile("[\\u4E00-\\u9FA5]+");
|
|
|
+ Matcher matcher = pattern.matcher(chineseWithLabels);
|
|
|
+
|
|
|
+ int lastMatchEnd = 0; // 上一个匹配结束的位置
|
|
|
+ while (matcher.find()) {
|
|
|
+ // 添加上一个匹配结束位置到当前匹配开始位置之间的非汉字字符到结果字符串
|
|
|
+ String nonChinesePart = chineseWithLabels.substring(lastMatchEnd, matcher.start());
|
|
|
+ sb.append(nonChinesePart);
|
|
|
+
|
|
|
+ // 将当前匹配到的汉字转换为拼音缩写并添加到结果字符串
|
|
|
+ String chinese = matcher.group();
|
|
|
+ String pinyinAbbreviation = getPinyinAbbreviation(chinese);
|
|
|
+ sb.append(pinyinAbbreviation);
|
|
|
+
|
|
|
+ // 更新上一个匹配结束的位置
|
|
|
+ lastMatchEnd = matcher.end();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加最后一个匹配结束位置到字符串末尾之间的非汉字字符到结果字符串
|
|
|
+ String remainingNonChinesePart = chineseWithLabels.substring(lastMatchEnd);
|
|
|
+ sb.append(remainingNonChinesePart);
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getPinyinAbbreviation(String chinese) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (char c : chinese.toCharArray()) {
|
|
|
+ String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
|
|
|
+ if (pinyinArray != null) {
|
|
|
+ sb.append(pinyinArray[0].charAt(0));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
}
|
|
|
}
|