puzhibing
2023-08-01 98bc380ae322eaac2ee7e21d0c565e30eacce2b5
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package com.sinata.zuul.util;
 
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 基本数据处理工具类
 */
public class SinataUtil {
    /**
     * List集合分页<br/> 
     * 创建人:Mryang<br/>
     * 时间:2016年7月28日-下午2:58:14 <br/> 
     * @param <T>
     * @param pageNo
     * @param pageSize
     * @param list 
     * @throws Exception List<UserOrderList> <br/>
     */
    public static <T> List<T> listpage(int pageNo, int pageSize, List<T> list) throws Exception {
        List<T> result = new ArrayList<T>();
        if (list != null && list.size() > 0) {
            int allCount = list.size();
            if(pageNo > 1 && allCount < pageSize) {
                return new ArrayList<>();
            }
            int pageCount = (allCount + pageSize - 1) / pageSize;
            if (pageNo >= pageCount) {
                pageNo = pageCount;
            }
            int start = (pageNo - 1) * pageSize;
            int end = pageNo * pageSize;
            if (end >= allCount) {
                end = allCount;
            }
            for (int i = start; i < end; i++) {
                result.add(list.get(i));
            }
        }
        return (result != null && result.size() > 0) ? result : new ArrayList<T>();
    }
 
    /**
     * Double类型取整
     * @param num
     * @return
     */
    public static String doubleTrans(double num) {
        return String.valueOf((long) num);
    }
 
    /**
     * Double类型保留1位小数
     * 
     * @param num
     * @return
     */
    public static String doubleRetainOne(double num) {
        DecimalFormat dfs = new DecimalFormat("0.0");
        return dfs.format(num);
    }
 
    /**
     * Double类型保留2位小数
     * 
     * @param num
     * @return
     */
    public static String doubleRetainTwo(double num) {
        DecimalFormat dfs = new DecimalFormat("0.00");
        String.format("%.2f", num);
        return dfs.format(num);
    }
 
    /**
     * Double类型保留1位小数(四舍五入)
     * 
     * @param num
     * @return
     */
    public static String doubleForwardOne(double num) {
        return String.format("%.1f", num);
    }
 
    /**
     * Double类型保留2位小数(四舍五入)
     * 
     * @param num
     * @return
     */
    public static String doubleForwardTwo(double num) {
        return String.format("%.2f", num);
    }
 
    /**
     * 字符串转换成Ascii
     * 
     * @param value
     * @return
     */
    public static String stringToAscii(String value) {
        StringBuffer sbu = new StringBuffer();
        char[] chars = value.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i != chars.length - 1) {
                sbu.append((int) chars[i]);
            } else {
                sbu.append((int) chars[i]);
            }
        }
        return sbu.toString();
    }
 
    /**
     * 小数转换为百分比
     *  
     * @param decimal
     * @return
     * @author TaoNingBo
     */
    public static String decTurnPercent(double decimal) {
        NumberFormat num = NumberFormat.getPercentInstance();
        num.setMaximumIntegerDigits(3);
        num.setMaximumFractionDigits(2);
        return num.format(decimal);
    }
 
    /**
     * Ascii转换成字符串
     * 
     * @param value
     * @return
     */
    public static String asciiToString(String value) {
        String[] chars = value.split(",");
        StringBuffer sbu = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            sbu.append((char) Integer.parseInt(chars[i]));
        }
        return sbu.toString();
    }
 
    /**
     * 字符串转换unicode
     * 
     * @param string
     * @return
     * @author TaoNingBo
     */
    public static String string2Unicode(String string) {
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            // 取出每一个字符
            char c = string.charAt(i);
            // 转换为unicode
            unicode.append("\\u" + Integer.toHexString(c));
        }
        return unicode.toString();
    }
 
    /**
     * unicode 转字符串
     * 
     * @param unicode
     * @return
     * @author TaoNingBo
     */
    public static String unicode2String(String unicode) {
        StringBuffer string = new StringBuffer();
        String[] hex = unicode.split("\\\\u");
        for (int i = 1; i < hex.length; i++) {
            // 转换出每一个代码点
            int data = Integer.parseInt(hex[i], 16);
            // 追加成string
            string.append((char) data);
        }
        return string.toString();
    }
 
    /**
     * 字符串编码转换的实现方法
     * 
     * @param str
     *            待转换编码的字符串
     * @param newCharset
     *            目标编码
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String changeCharset(String str, String newCharset) throws UnsupportedEncodingException {
        if (str != null) {
            // 用默认字符编码解码字符串。
            byte[] bs = str.getBytes();
            // 用新的字符编码生成字符串
            return new String(bs, newCharset);
        }
        return null;
    }
 
    /**
     * 注: \n 回车( ) \t 水平制表符( ) \s 空格(\u0008) \r 换行( )
     * 
     * @param str
     * @return
     */
    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
 
    /**
     * 判断该字符串不能为空
     * 
     * @param str
     * @return
     * @author TaoNingBo
     */
    public static boolean isNotEmpty(Object str) {
        return !isEmpty(str);
    }
    
    
    public static boolean isNotEmptyUndefined(Object str) {
        return !isEmpty(str) && !str.toString().equals("undefined");
    }
 
    /**
     * 字符串编码转换的实现方法
     * 
     * @param str
     *            待转换编码的字符串
     * @param oldCharset
     *            原编码
     * @param newCharset
     *            目标编码
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String changeCharset(String str, String oldCharset, String newCharset) throws UnsupportedEncodingException {
        if (str != null) {
            // 用旧的字符编码解码字符串。解码可能会出现异常。
            byte[] bs = str.getBytes(oldCharset);
            // 用新的字符编码生成字符串
            return new String(bs, newCharset);
        }
        return null;
    }
 
    /**
     * 给手机号码加分割符
     * 
     * @param phone
     * @return
     * @author TaoNingBo
     */
    public static String splitPhone(String phone) {
        if (isNotEmpty(phone)) {
            String strone = phone.substring(0, 3);
            String strtwo = phone.substring(strone.length(), 7);
            String strthree = phone.substring(strtwo.length() + strone.length(), phone.length());
            return strone + "-" + strtwo + "-" + strthree;
        }
        return "";
    }
 
    /**
     * 非空判断
     * 
     * @param str
     * @return
     * @author TaoNingBo
     */
    public static boolean isEmpty(Object str) {
        return str == null || str.toString().length() == 0 || str.equals("") || str.toString().matches("\\s*");
    }
 
    /**
     * 把米转换成公里
     * 
     * @param km
     * @return
     * @author TaoNingBo
     */
    public static Double kmTransKilo(Integer m) {
        return Math.round(m / 100d) / 10d;
    }
    
    /**
     * 将List<{@link Object}>转换成List<{@link T}>
     * 
     * @param list
     *            将要转换的对象
     * @param clazs
     *            需要转换的泛型对象
     * @return
     * @author TaoNingBo
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> fromToObject(List<?> list, Class<T> clazs) {
        List<T> t = new ArrayList<T>();
        for (Object object : list) {
            t.add((T) object);
        }
        return t;
    }
    
     /**
     * 生成 uuid, 即用来标识一笔单,也用做 nonce_str
     * @return
     */
    public static String generateUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
    }
    
    /**
     * 将List<{@link Object}>转换成List<{@link Map<String, Object>}>
     * 
     * @param list
     * @return
     * @author TaoNingBo
     */
    @SuppressWarnings("unchecked")
    public static List<Map<String, Object>> fromToObject_M(List<?> list) {
        List<Map<String, Object>> t = new ArrayList<Map<String, Object>>();
        for (Object object : list) {
            t.add((Map<String, Object>) object);
        }
        return t;
    }
    
 
/**
 * 将对象中的null转换为空
 * @param obj
 * @return
 * @throws IllegalAccessException
 */
 
public static  Object checkObjFieldIsNull(Object obj) throws IllegalAccessException {
    if(obj!=null){
        for(java.lang.reflect.Field f : obj.getClass().getDeclaredFields()){
            f.setAccessible(true);
            if(f.get(obj) == null){
                if(f.getType()==String.class){
                    f.set(obj, "");
                }
                if(f.getType()==Double.class){
                    f.set(obj, 0d);
                }
                if(f.getType()==Integer.class){
                    f.set(obj, 0);
                }
                if(f.getType()==Date.class){
                    f.set(obj, new Date());
                }
            }
        }
        return obj;
    }
    return obj;
}
/**
 * 获取六位标识码
 * @param length
 * @return
 */
public static String createRandomCharData()
{
    StringBuilder sb=new StringBuilder();
    Random rand=new Random();//随机用以下三个随机生成器
    Random randdata=new Random();
    int data=0;
    for(int i=0;i<6;i++)
    {
        int index=rand.nextInt(3);
        //目的是随机选择生成数字,大小写字母
        switch(index)
        {
        case 0:
             data=randdata.nextInt(10);//仅仅会生成0~9
             sb.append(data);
            break;
        case 1:
            data=randdata.nextInt(26)+65;//保证只会产生65~90之间的整数
            sb.append((char)data);
            break;
        case 2:
            data=randdata.nextInt(26)+97;//保证只会产生97~122之间的整数
            sb.append((char)data);
            break;
        }
    }
    String result=sb.toString().toLowerCase();
    return result;
}
}