forked from AlEinstein/javaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatUtil.java
More file actions
306 lines (263 loc) · 8.16 KB
/
Copy pathFormatUtil.java
File metadata and controls
306 lines (263 loc) · 8.16 KB
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
package common.utils;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
/**
* Create by mulin on 2018/12/26.
*/
public class FormatUtil {
/**
* 判断是否为整数(包括负数)
*/
public static boolean isNumber(Object arg) {
return NumberBo(0, toString(arg));
}
/**
* 判断是否为小数(包括整数,包括负数)
*/
public static boolean isDecimal(Object arg) {
return NumberBo(1, toString(arg));
}
/**
* 判断是否为空 ,为空返回true
*/
public static boolean isEmpty(Object arg) {
return toStringTrim(arg).length() == 0 ? true : false;
}
/**
* Object 转换成 Int 转换失败 返回默认值 0
* 使用:toInt(值,默认值[选填])
*/
public static int toInt(Object... args) {
int def = 0;
if (args != null) {
String str = toStringTrim(args[0]);
// 判断小数情况。舍弃小数位
int stri = str.indexOf('.');
str = stri > 0 ? str.substring(0, stri) : str;
if (args.length > 1) {
def = Integer.parseInt(args[args.length - 1].toString());
}
if (isNumber(str)) {
return Integer.parseInt(str);
}
}
return def;
}
/**
* Object 转换成 Long 转换失败 返回默认值 0
* 使用:toLong(值,默认值[选填])
*/
public static long toLong(Object... args) {
Long def = 0L;
if (args != null) {
String str = toStringTrim(args[0]);
if (args.length > 1) {
def = Long.parseLong(args[args.length - 1].toString());
}
if (isNumber(str)) {
return Long.parseLong(str);
}
}
return def;
}
/**
* Object 转换成 Double 转换失败 返回默认值 0
* 使用:toDouble(值,默认值[选填])
*/
public static double toDouble(Object... args) {
double def = 0;
if (args != null) {
String str = toStringTrim(args[0]);
if (args.length > 1) {
def = Double.parseDouble(args[args.length - 1].toString());
}
if (isDecimal(str)) {
return Double.parseDouble(str);
}
}
return def;
}
/**
* Object 转换成 BigDecimal
* 若转换失败 则返回默认值 0
* 使用:toDecimal(值,默认值[选填])
* 【特别注意】: new BigDecimal(Double) 会有误差,得先转String
* 【特别注意】: 由于精度问题,不能涉及到其他数据类型 :float,double..浮点数类型
* 【特别注意】:涉及到金额的承载及操作,不允许用浮点数!!
*/
public static BigDecimal toDecimal(Object... args) {
BigDecimal def = BigDecimal.ZERO;
if (args != null) {
String str = toStringTrim(args[0]);
if (args.length > 1) {
def = new BigDecimal(args[args.length - 1].toString());
}
if (isDecimal(str)) {
return new BigDecimal(str);
}
}
return def;
}
/**
* Object 转换成 Boolean
* 若转换失败 则返回默认值 false
* 使用:toBoolean(值,默认值[选填])
* true=[1,true,"true","ok","yes"]
*/
public static boolean toBoolean(Object bo) {
String bool = toString(bo);
if (isEmpty(bool) || (!bool.equals("1") && !bool.equalsIgnoreCase("true")
&& !bool.equalsIgnoreCase("ok") && !bool.equalsIgnoreCase("yes"))) {
return false;
} else {
return true;
}
}
/**
* Object 转换成 String
* 为null则返回空字符
* 使用:toString(值,默认值[选填])
*/
public static String toString(Object... args) {
String def = "";
if (args != null) {
if (args.length > 1) {
def = toString(args[args.length - 1]);
}
Object obj = args[0];
if (obj == null) {
return def;
}
return obj.toString();
} else {
return def;
}
}
/**
* Object 转换成 String [去除所有空格];
* 为null 返回空字符
* 使用:toStringTrim(值,默认值[选填])
*/
public static String toStringTrim(Object... args) {
String str = toString(args);
return str.replaceAll("\\s*", "");
}
/**
* 数字左边补0
* obj:要补0的数
* length:补0后的长度
*/
public static String leftPad(Object obj, int length) {
return String.format("%0" + length + "d", toInt(obj));
}
/**
* 小数 转 百分数
*/
public static String toPercent(Double str) {
StringBuffer sb = new StringBuffer(Double.toString(str * 100.0000d));
return sb.append("%").toString();
}
/**
* 百分数 转 小数
*/
public static Double toPercent2(String str) {
if (str.charAt(str.length() - 1) == '%')
return Double.parseDouble(str.substring(0, str.length() - 1)) / 100.0000d;
return 0d;
}
/**
* String 转 Money
*/
public static String toMoney(Object str, String MoneyType) {
DecimalFormat df = new DecimalFormat(MoneyType);
if (isDecimal(str))
return df.format(toDecimal(str)).toString();
return df.format(toDecimal("0.00")).toString();
}
/**
* List集合去除重复值 只能用于基本数据类型。
*/
public static List delMoreList(List list) {
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
return newList;
}
/**
* 字符串格式化
* 使用占位符:{}
*/
public static String formatParams(String message, Object[] params) {
StringBuffer msg = new StringBuffer();
String temp = "";
for (int i = 0; i < params.length + 1; i++) {
int j = message.indexOf("{}") + 2;
if (j > 1) {
temp = message.substring(0, j);
temp = temp.replaceAll("\\{\\}", FormatUtil.toString(params[i]));
msg.append(temp);
message = message.substring(j);
} else {
msg.append(message);
message = "";
}
}
return msg.toString();
}
/**
* 字符串格式化
* 使用占位符:${xxx} xxx为key
*/
public static String formatParams(String message, Map<String, String> params) {
for (Map.Entry<String, String> entry : params.entrySet()) {
message = message.replaceAll("\\$\\{" + entry.getKey() + "\\}", entry.getValue());
}
return message;
}
/**
* 判断是否是数字
* type: 0为整数,1为小数
*/
private static boolean NumberBo(int type, Object obj) {
if (isEmpty(obj)) {
return false;
}
int points = 0;
int chr = 0;
String str = toString(obj);
for (int i = str.length(); --i >= 0; ) {
chr = str.charAt(i);
if (chr < 48 || chr > 57) { // 判断数字
if (i == 0 && chr == 45) // 判断 - 号
return true;
if (i >= 0 && chr == 46 && type == 1) { // 判断 . 号
++points;
if (points <= 1) {
continue;
}
}
return false;
}
}
return true;
}
/**
* 转为JSON串
*/
public static String toJSONString(Object obj) {
return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue);
}
/**
* 获取UUID
*/
public static String getUUID() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
}