hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
package frame.util;
 
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
import frame.persist.DataBaseType;
 
public class Util {
 
    public static final String TRUE = "T";
    public static final String FALSE = "F";
    public static final String String_Return = "\r\n";
    public static final String String_Escape_newSpace = "\t";
    public static final String String_Escape_newLine = "\n";
    public static final String String_Empty = "";
    public static final String Sql_Empty = "''";
    public static final String Default_Patter = "(?<=@\\{)(.+?)(?=\\})";
    public static final String Integer_Patter = "^-?[1-9]\\d*$";
    public static final String Double_Patter = "^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$";
    public static final String Separator = "-";
    public static final String SubSeparator = "_";
    public static final String comma = " , ";
    public static final String with = "&";
    public static final String dollar = "$";
    public static final String Star = "*";
    public static final String RMB = "¥";
    public static final String ait = "@";
    public static final String semicolon = ";";
    public static final String defaultFilter = " 1 = 1 ";
    public static final String Dot = ".";
    public static final String Spilt_Dot = "[.]";
    public static final String Spilt_Line = "\\|";
    public static final String Spilt_Star = "\\*";
    public static final String Spilt_Slash = "\\\\";
    public static final String Spilt_Brackets = "\\[\\]";
    public static final String Equal = "=";
    public static final String And = " and ";
    public static final String Or = " or ";
    public static final String Standrad_And = "and";
    public static final String Standrad_Or = "or";
    public static final String Percentage = "%";
    public static String like = " like ";
    public static String Standrad_like = "like";
    public static String unEqual = " <> ";
    public static String Standrad_unEqual = "<>";
    public static final String String_Space = " ";
    public static final String windows_slash = "/";
    public static final String java_slash = "\\";
    public static final String sql_slash = "\\\\";
 
    public static String newShortGUID() {
        UUID uuid = UUID.randomUUID();
        String strGUID;
        String shortGUID;
 
        strGUID = uuid.toString();
        shortGUID = strGUID.substring(0, 8) + strGUID.substring(9, 13) + strGUID.substring(14, 18) + strGUID.substring(19, 23)
                + strGUID.substring(24, 36);
 
        return shortGUID;
    }
 
    public static String quotedStr(String str) {
        if (str != null)
            return "'" + str + "'";
        else
            return "''";
    }
    public static String quotedEqualStr(String key, String value) {
        if (!Util.isEmptyStr(key) && !Util.isEmptyStr(value))
            return key + String_Space + Equal + String_Space + Util.quotedStr(value);
        else
            return null;
    }
 
    public static String doubleQuotedStr(String str) {
        if (str != null)
            return "\"" + str + "\"";
        else
            return "\"\"";
    }
 
    public static String quotedLikeStr(String str) {
        if (str != null)
            return "'%" + str + "%'";
        else
            return "''";
    }
 
    public static String bracketStr(String str) {
        if (str != null)
            return "(" + str + ")";
        else
            return "()";
    }
 
    public static String stringJoin(String... strings) {
        StringBuilder stringBuilder = new StringBuilder();
        for (String s: strings) {
            stringBuilder.append(s);
        }
        return  stringBuilder.toString();
    }
    public static String stringJoin(Object... strings) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Object s: strings) {
            stringBuilder.append(s.toString());
        }
        return  stringBuilder.toString();
    }
 
    public static String[] split(String str) {
        if (str == null) {
            return new String[0];
        }
        
        return str.replace(",", ";").replace(",", ";").replace(";", ";").split(";");
    }
 
    public static String newDBDateString() throws Exception {
        Date date = new Date();
        return newDBDateString(date);
    }
 
    public static String newDBDateString(Date date) throws Exception {
        DataBaseType dbType = DataBaseType.MySQL;
 
        if (DataBaseType.Oracle == dbType) {
            return newOracleDateString(date);
        }
        else if (DataBaseType.SQLServer == dbType) {
            return newSqlServerDateString(date);
        }
        else if (DataBaseType.MySQL == dbType) {
            return newMySqlDateString(date);
        }
        else {
            return DataTimeToString(date);
        }
    }
 
    public static String toOracleDataStr(String dataStr) {
        return "to_date('" + dataStr + "','YYYY-MM-DD HH24:MI:SS')";
    }
 
    public static String toMySQLDateStr(Date value) {
        return DataTimeToString(value, "yyyy-MM-dd HH:mm:ss");
    }
 
    public static String DataTimeToString(Date value) {
        return DataTimeToString(value, "yyyy-MM-dd HH:mm:ss");
    }
 
    public static String DataTimeToString(Date value, String format) {
        if (value == null) {
            return null;
        }
 
        String result = "";
        DateFormat dateFormat = new SimpleDateFormat(format);
        result = dateFormat.format(value);
 
        return result;
    }
 
    public static String newDateStr() {
        return newDateTimeStr("yyyy-MM-dd");
    }
 
    public static String newDateTimeStr() {
        return newDateTimeStr("yyyy-MM-dd kk:mm:ss");
    }
 
    public static String newDateTimeStr(String fomater) {
        return getDateTimeStr(new Date(), fomater);
    }
 
    public static String getDateTimeStr(Date date, String fomater) {
        String result = "";
        DateFormat dateFormat = new SimpleDateFormat(fomater);
        result = dateFormat.format(date);
 
        return result;
    }
 
    public static String booleanToStr(boolean value) {
        if (value)
            return "T";
        else
            return "F";
    }
 
    public static boolean isEmptyStr(Object str) {
        boolean result = false;
 
        if ((str == null) || ("".equals(str)))
            result = true;
 
        return result;
    }
    
    public static String IfEmpetyStr(String str, String value) {
        if (isEmptyStr(str)) {
            return value;
        }
        
        return str;
    }
 
    public static boolean isNull(Object object) {
        if ((object == null))
            return true;
        if (object instanceof String) {
            return isNull((String)object);
        }
 
        return false;
    }
 
    public static boolean isNull(String value) {
        if ((value == null))
            return true;
 
        if ("".equals(value)) {
            return true;
        }
 
        if (value.length() == 4) {
            value = value.toLowerCase();
            return "null".equals(value);
        }
 
        return false;
    }
 
    public static String UTF8decode(String str) {
        if (!isUTF8Encoding(str))
            return str;
        byte[] bytes = str.getBytes();
        ByteBuffer bb = ByteBuffer.wrap(bytes);
        Charset csets = Charset.forName("UTF-8");
        CharBuffer c = csets.decode(bb);
        return c.toString();
    }
 
    private static boolean isUTF8Encoding(String str) {
        byte[] bytes = str.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            int byteLen = Byte.toString(bytes[i]).length();
            if (byteLen == 4)
                return true;
            else
                continue;
        }
        return false;
    }
 
    public static boolean StringToBoolean(String value) {
        if (value != null) {
            value = value.toLowerCase();
 
            if (value.equals("t")) {
                return true;
            }
            else if (value.equals("y")) {
                return true;
            }
            else if (value.equals("true")) {
                return true;
            }
            else if (value.equals("yes")) {
                return true;
            }
            else {
                return false;
            }
        }
        else
            return false;
    }
 
    public static int StringToInt(String value, int defaultValue) {
        if (value != null) {
            try {
                Double doubleValue = Double.valueOf(value);
                return doubleValue.intValue();
            }
            catch (Exception e) {
                return defaultValue;
            }
        }
        else
            return defaultValue;
    }
 
    public static BigDecimal StringToBigDecimal(String value, BigDecimal defaultValue) {
        if (value == null) {
            return defaultValue;
        }
 
        try {
            BigDecimal decimalValue = BigDecimal.valueOf(Double.valueOf(value));
            return decimalValue;
        }
        catch (Exception e) {
            return defaultValue;
        }
    }
 
    public static Date StringToDate(String str) throws ParseException {
        if (Util.isEmptyStr(str)) {
            return null;
        }
 
        Date result = null;
        String fomater = null;
        str = str.replace('T', ' ');
 
        if (str.indexOf("/") == 4) {
            fomater = "yyyy/MM/dd";
        }
        else if (str.indexOf("/") == 2 || str.indexOf("/") == 1) {
            fomater = "MM/dd/yyyy";
        }
        else if (str.indexOf("-") == 2 || str.indexOf("-") == 1) {
            fomater = "MM-dd-yyyy";
        }
        else if (str.indexOf("-") == 4 && str.indexOf(":") < 0) {
            fomater = "yyyy-MM-dd";
        }
        else if (str.indexOf("-") == 4 && str.indexOf(":") > 0) {
            if (str.split(":").length == 3) {
                fomater = "yyyy-MM-dd HH:mm:ss";
            }
            else {
                str = str + ":00";
                fomater = "yyyy-MM-dd HH:mm:00";
            }
 
        }
        else if (str.indexOf(".") == 2 || str.indexOf(".") == 1) {
            fomater = "MM.dd.yyyy";
        }
        else if (str.indexOf(".") == 4) {
            fomater = "yyyy.MM.dd";
        }
        else if (str.indexOf("-") < 0 && str.indexOf("/") < 0) {
            fomater = "yyyyMMdd";
        }
 
        DateFormat dateFormat = new SimpleDateFormat(fomater);
        result = dateFormat.parse(str);
 
        return result;
    }
 
    public static Date doubleToDate(Double value) throws ParseException {
        Date result = null;
 
        if (value != null) {
            if (value > 195000 && value <= 210001) {
                value = value * 100 + 01;
            }
 
            if (value >= 19500101 && value <= 21000101) {
                String value_Str = String.valueOf(value.intValue());
                result = Util.StringToDate(value_Str);
            }
            else if (value > (1950 - 1900) * 365 && value < (2100 - 1900) * 365) {
                int dateValue = value.intValue();
                double secValue = value - dateValue;
                Date dayDate = intToDate(dateValue);
                long sec = Math.round(secValue * 24 * 3600 * 1000);
 
                result = new Date();
                result.setTime(dayDate.getTime() + sec);
                return result;
            }
        }
 
        return result;
    }
 
    public static Date intToDate(int value) {
        Calendar result = Calendar.getInstance();
        result.set(Calendar.YEAR, 1900);
        result.set(Calendar.MONTH, 0);
        result.set(Calendar.DAY_OF_MONTH, 1);
        result.set(Calendar.HOUR_OF_DAY, 0);
        result.set(Calendar.MINUTE, 0);
        result.set(Calendar.SECOND, 0);
 
        result.add(Calendar.DATE, value - 2);
        return result.getTime();
    }
 
    public static int getArrayContentSize(Object[] datas) {
        int result = 0;
 
        for (int i = 0; i < datas.length; i++) {
            if (datas[i] != null) {
                result++;
            }
        }
 
        return result;
    }
 
    public static String deleteSuffix(String name) {
        String result = null;
 
        if (!isEmptyStr(name)) {
            int pos = name.lastIndexOf(".");
            result = name.substring(0, pos);
        }
 
        return result;
    }
 
    public static String[] mergeArray(String[] array1, String[] array2) {
        if (array1 == null) {
            return array2;
        }
 
        if (array2 == null) {
            return array2;
        }
 
        List<String> set = new ArrayList<String>(array1.length + array2.length);
 
        for (int i = 0; i < array1.length; i++) {
            set.add(array1[i]);
        }
 
        for (int i = 0; i < array2.length; i++) {
            if (!set.contains(array2[i])) {
                set.add(array2[i]);
            }
        }
 
        String[] result = new String[0];
        return set.toArray(result);
    }
 
    public static String newOracleDateString(Date date) {
        String nowStr = "";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        nowStr = dateFormat.format(date);
 
        return "to_date('" + nowStr + "','YYYY-MM-DD HH24:MI:SS')";
    }
 
    public static String newMySqlDateString(Date date) {
        String nowStr = "";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        nowStr = dateFormat.format(date);
 
        return "('" + nowStr + "')";
    }
 
    public static String newSqlServerDateString(Date date) {
        String nowStr = "";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        nowStr = dateFormat.format(date);
 
        return "('" + nowStr + "')";
    }
 
    public static boolean isSameString(String value1, String value2) {
        if (value1 == null) {
            if (value2 == null) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            if (value2 == null) {
                return false;
            }
            else {
                return value1.equals(value2);
            }
        }
    }
 
    public static boolean isSameStringIgnoreCase(String value1, String value2) {
        if (value1 == null) {
            if (value2 == null) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            if (value2 == null) {
                return false;
            }
            else {
                return value1.equalsIgnoreCase(value2);
            }
        }
    }
 
    public static String toLowerCase(String name, String defaultValue) {
        if (name == null) {
            return defaultValue;
        }
        else {
            return name.toLowerCase();
        }
    }
 
    public static String getExceptionStack(Exception e) {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(outStream);
        e.printStackTrace(printStream);
 
        return outStream.toString();
    }
 
    public static Date getSpecialDayOffToday(int dayCount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + dayCount);
        return calendar.getTime();
    }
 
    public static String getPassWord(int length) {
        int[] array = new int[length];
        char[] chars = new char[length];
        StringBuilder str = new StringBuilder();
        int temp = 0;
        for (int i = 0; i < length; i++) {
            while (true) {
                temp = (int) (Math.random() * 1000);
                if (temp >= 48 && temp <= 57)
                    break;
                if (temp >= 65 && temp <= 90)
                    break;
                if (temp >= 97 && temp <= 122)
                    break;
            }
 
            array[i] = temp;
            chars[i] = (char) array[i];
            str.append(chars[i]);
        }
 
        return str.toString();
    }
 
    public static String escapeQuoted(String filter) {
        if (filter == null) {
            return filter;
        }
 
        int length = filter.length();
 
        if (length <= 1) {
            return filter;
        }
 
        char first = filter.charAt(0);
        char last = filter.charAt(length - 1);
 
        if (('\'' == first || '"' == first) && (first == last)) {
            filter = filter.substring(1, length - 1);
            filter = filter.trim();
            return filter;
        }
 
        return filter;
    }
 
    public static int[] getCurYearMonth() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        return new int[] { year, month };
    }
 
    public static String getTimeStamp(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }
 
    public static String getFileExt(String filename) {
        int pos = filename.lastIndexOf(".");
        String ext = filename.substring(pos);
        return ext;
    }
 
    public static int getMonth(String value) throws ParseException {
        Date date = StringToDate(value);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }
 
    public static int getYear(String value) throws ParseException {
        Date date = StringToDate(value);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }
 
    public static String joinPath(String parent, String path) {
        if (isEmptyStr(parent)) {
            return path;
        }
        
        parent = parent.replace("\\", "/");
        path = path.replace("\\", "/");
        
        if ('/' == parent.charAt(parent.length() - 1)) {
            parent = parent.substring(0, parent.length() - 1);
        }
        
        if ('/' == path.charAt(0)) {
            path = path.substring(1);
        }
        
        return parent + "/" + path;
    }
 
    public static String deleteInvisiableChar(String value) {
        if (value == null) {
            return null;
        }
 
        return value.replace("\r", "").replace("\n", "");
    }
 
    public static boolean isDigital(String value) {
        if (value == null) {
            return false;
        }
        
        for (int i = 0; i < value.length(); i++) {
            char c = value.charAt(i);
            if (!Character.isDigit(c)) {
                return false;
            }
        }
        
        return true;
    }
    
}