forked from xuwujing/java-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyTools.java
More file actions
919 lines (841 loc) · 21.8 KB
/
Copy pathMyTools.java
File metadata and controls
919 lines (841 loc) · 21.8 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
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
package com.pancm.utils;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.codec.binary.Base64;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
*
* @Title: MyTools
* @Description:常用工具类
* @Version:1.0.1
* @author pancm
* @date 2017年9月26日
*/
public final class MyTools {
/** 时间格式包含毫秒 */
private static final String sdfm = "yyyy-MM-dd HH:mm:ss SSS";
/** 普通的时间格式 */
private static final String sdf = "yyyy-MM-dd HH:mm:ss";
/** 时间戳格式 */
private static final String sd = "yyyyMMddHHmmss";
/** 检查是否为整型 */
private static Pattern p = Pattern.compile("^\\d+$");
/**
* 判断String类型的数据是否为空 null,""," " 为true "A"为false
*
* @return boolean
*/
public static boolean isEmpty(String str) {
return (null == str || str.trim().length() == 0);
}
/**
* 判断String类型的数据是否为空 null,"", " " 为false "A", 为true
*
* @return boolean
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
/**
* 判断list类型的数据是否为空 null,[] 为 true
*
* @return boolean
*/
public static boolean isEmpty(List<?> list) {
return (null == list || list.size() == 0);
}
/**
* 判断list类型的数据是否为空 null,[] 为 false
*
* @return boolean
*/
public static boolean isNotEmpty(List<?> list) {
return !isEmpty(list);
}
/**
* 判断Map类型的数据是否为空 null,[] 为true
*
* @return boolean
*/
public static boolean isEmpty(Map<?, ?> map) {
return (null == map || map.size() == 0);
}
/**
* 判断map类型的数据是否为空 null,[] 为 false
*
* @return boolean
*/
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
/**
* 判断JSONObject类型的数据是否为空 null,[] 为true
*
* @return boolean
*/
public static boolean isEmpty(JSONObject json) {
return (null == json || json.size() == 0);
}
/**
* 判断json类型的数据是否为空 null,[] 为 false
*
* @return boolean
*/
public static boolean isNotEmpty(JSONObject json) {
return !isEmpty(json);
}
/**
* 字符串反转 如:入参为abc,出参则为cba
*
* @param str
* @return
*/
public static String reverse(String str) {
if (isEmpty(str)) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
/**
* 获取当前long类型的的时间
*
* @return long
*/
public static long getNowLongTime() {
return System.currentTimeMillis();
}
/**
* long类型的时间转换成 yyyyMMddHHmmss String类型的时间
*
* @param lo
* long类型的时间
* @return
*/
public static String longTime2StringTime(long lo) {
return longTime2StringTime(lo, sd);
}
/**
* long类型的时间转换成自定义时间格式
*
* @param lo
* long类型的时间
* @param format
* 时间格式
* @return String
*/
public static String longTime2StringTime(long lo, String format) {
return new SimpleDateFormat(format).format(lo);
}
/**
* String类型的时间转换成 long
*
* @param lo
* @return String
* @throws ParseException
*/
public static long stringTime2LongTime(String time, String format) throws ParseException {
if (isEmpty(format)) {
format = sdf;
}
if (isEmpty(time)) {
time = getNowTime(format);
}
SimpleDateFormat sd = new SimpleDateFormat(format);
Date date = sd.parse(time);
return date.getTime();
}
/**
* 格式化时间
*
* @param format1
* 之前的 时间格式
* @param format2
* 之后的 时间格式
* @param time
* 时间
* @return String
* @throws ParseException
*/
public static String formatTime(String format1, String format2, String time) throws ParseException {
SimpleDateFormat d1 = new SimpleDateFormat(format1);
SimpleDateFormat d2 = new SimpleDateFormat(format2);
time = d2.format(d1.parse(time));
return time;
}
/**
* 时间补全 例如将2018-04-04补全为2018-04-04 00:00:00.000
*
* @param time
* 补全的时间
* @return
*/
public static String complementTime(String time) {
return complementTime(time, sdfm, 1);
}
/**
* 时间补全 例如将2018-04-04补全为2018-04-04 00:00:00.000
*
* @param time
* 补全的时间
* @param format
* 补全的格式
* @param type
* 类型 1:起始;2:终止
* @return
*/
public static String complementTime(String time, String format, int type) {
if (isEmpty(time) || isEmpty(format)) {
return null;
}
int tlen = time.length();
int flen = format.length();
int clen = flen - tlen;
if (clen <= 0) {
return time;
}
StringBuffer sb = new StringBuffer(time);
if (clen == 4) {
if (type == 1) {
sb.append(".000");
} else {
sb.append(".999");
}
} else if (clen == 9) {
if (type == 1) {
sb.append(" 00:00:00");
} else {
sb.append(" 23:59:59");
}
} else if (clen == 13) {
if (type == 1) {
sb.append(" 00:00:00.000");
} else {
sb.append(" 23:59:59.999");
}
}
return sb.toString();
}
/**
* 获取当前String类型的的时间 使用默认格式 yyyy-MM-dd HH:mm:ss
*
* @return String
*/
public static String getNowTime() {
return getNowTime(sdf);
}
/**
* 获取当前String类型的的时间(自定义格式)
*
* @param format
* 时间格式
* @return String
*/
public static String getNowTime(String format) {
return new SimpleDateFormat(format).format(new Date());
}
/**
* 获取当前Timestamp类型的的时间
*
* @return Timestamp
*/
public static Timestamp getTNowTime() {
return new Timestamp(getNowLongTime());
}
/**
* 获取的String类型的当前时间并更改时间
*
* @param number
* 要更改的的数值
* @param format
* 更改时间的格式 如yyyy-MM-dd HH:mm:ss
* @param type
* 更改时间的类型 时:h; 分:m ;秒:s
* @return String
*/
public static String changeTime(int number, String format, String type) {
return changeTime(number, format, type, "");
}
/**
* 获取的String类型时间并更改时间
*
* @param number
* 要更改的的数值
* @param format
* 更改时间的格式
* @param type
* 更改时间的类型 。时:h; 分:m ;秒:s
* @param time
* 更改的时间 没有则取当前时间
* @return String
*/
public static String changeTime(int number, String format, String type, String time) {
if (isEmpty(time)) { // 如果没有设置时间则取当前时间
time = getNowTime(format);
}
SimpleDateFormat format1 = new SimpleDateFormat(format);
Date d = null;
Calendar ca = null;
String backTime = null;
try {
d = format1.parse(time);
ca = Calendar.getInstance(); // 定义一个Calendar 对象
ca.setTime(d);// 设置时间
if ("h".equals(type)) {
ca.add(Calendar.HOUR, number);// 改变时
} else if ("m".equals(type)) {
ca.add(Calendar.MINUTE, number);// 改变分
} else if ("s".equals(type)) {
ca.add(Calendar.SECOND, number);// 改变秒
}
backTime = format1.format(ca.getTime()); // 转化为String 的格式
} catch (Exception e) {
e.printStackTrace();
}
return backTime;
}
/**
* 两个日期带时间比较 第二个时间大于第一个则为true,否则为false
*
* @param String
* @return boolean
* @throws ParseException
*/
public static boolean isCompareDay(String time1, String time2, String format) {
if (isEmpty(format)) {// 如果没有设置格式使用默认格式
format = sdf;
}
SimpleDateFormat s1 = new SimpleDateFormat(format);
Date t1 = null;
Date t2 = null;
try {
t1 = s1.parse(time1);
t2 = s1.parse(time2);
return t2.after(t1);// 当 t2 大于 t1 时,为 true,否则为 false
} catch (ParseException e) {
e.printStackTrace();
return false;
}
}
/**
* 获取几天之前的时间
*
* @since 1.8
* @param day
* @return
*/
public static String getMinusDays(int day) {
return getMinusDays(day, sdf);
}
/**
* 获取几天之前的时间
*
* @since 1.8
* @param day
* @param format
* @return
*/
public static String getMinusDays(int day, String format) {
return LocalDateTime.now().minusDays(day).format(DateTimeFormatter.ofPattern(format));
}
/**
* 获取几天之后的时间
*
* @since 1.8
* @param day
* @return
*/
public static String getPlusDays(int day) {
return getPlusDays(day, sdf);
}
/**
* 获取几天之后的时间
*
* @since 1.8
* @param day
* @param format
* @return
*/
public static String getPlusDays(int day, String format) {
return LocalDateTime.now().plusDays(day).format(DateTimeFormatter.ofPattern(format));
}
/**
* 获取几天之后的时间
*
* @since 1.8
* @param day
* @return
*/
public static String getPlusMonths(int month) {
return getPlusMonths(month, sdf);
}
/**
* 获取几月之后的时间
*
* @since 1.8
* @param day
* @param format
* @return
*/
public static String getPlusMonths(int month, String format) {
return LocalDateTime.now().plusMonths(month).format(DateTimeFormatter.ofPattern(format));
}
/**
* 增加月份
*
* @param time
* 格式为yyyy-MM-dd
* @param month
* 增加月份
* @return
*/
public static String addPlusMonths(String time, int month) {
return LocalDate.parse(time).plusMonths(month).toString();
}
/**
* 时间相比得月份 如果是201711和201801相比,返回的结果是2 前面的时间要小于后面的时间
*
* @param month
* 格式为yyyyMM
* @param toMonth
* 格式为yyyyMM
* @since jdk 1.8
* @return
*/
public static int diffMonth(String month, String toMonth) {
int year1 = Integer.parseInt(month.substring(0, 4));
int month1 = Integer.parseInt(month.substring(4, 6));
int year2 = Integer.parseInt(month.substring(0, 4));
int month2 = Integer.parseInt(month.substring(4, 6));
LocalDate ld1 = LocalDate.of(year1, month1, 01);
LocalDate ld2 = LocalDate.of(year2, month2, 01);
return Period.between(ld1, ld2).getMonths();
}
/**
* 判断是否为整型
*
* @param String
* @return boolean
*/
public static boolean isInteger(String str) {
Matcher m = p.matcher(str);
return m.find();
}
/**
* 自定义位数产生随机数字
*
* @param int
* @return String
*/
public static String random(int count) {
char start = '0';
char end = '9';
Random rnd = new Random();
char[] result = new char[count];
int len = end - start + 1;
while (count-- > 0) {
result[count] = (char) (rnd.nextInt(len) + start);
}
return new String(result);
}
/**
* 获取自定义长度的随机数(含字母)
*
* @param len
* 长度
* @return String
*/
public static String random2(int len) {
int random = Integer.parseInt(random(5));
Random rd = new Random(random);
final int maxNum = 62;
StringBuffer sb = new StringBuffer();
int rdGet;// 取得随机数
char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9' };
int count = 0;
while (count < len) {
rdGet = Math.abs(rd.nextInt(maxNum));// 生成的数最大为62-1
if (rdGet >= 0 && rdGet < str.length) {
sb.append(str[rdGet]);
count++;
}
}
return sb.toString();
}
/**
* 获取本机ip
*
* @return String
* @throws UnknownHostException
*/
public static String getLocalHostIp() throws UnknownHostException {
return InetAddress.getLocalHost().getHostAddress();
}
/**
* Object 转换为 String
*
* @param list
* @return String
*/
public static String toString(Object obj) {
return JSON.toJSONString(obj);
}
/**
* JSON 转换为 JavaBean
*
* @param json
* @param t
* @return <T>
*/
public static <T> T toBean(JSONObject json, Class<T> t) {
return JSON.toJavaObject(json, t);
}
/**
* JSON 字符串转换为 JavaBean
*
* @param str
* @param t
* @return <T>
*/
public static <T> T toBean(String str, Class<T> t) {
return JSON.parseObject(str, t);
}
/**
* JSON 字符串 转换成JSON格式
*
* @param obj
* @return JSONObject
*/
public static JSONObject toJson(String str) {
if (isEmpty(str)) {
return new JSONObject();
}
return JSON.parseObject(str);
}
/**
* JavaBean 转化为JSON
*
* @param t
* @return
*/
public static JSONObject toJson(Object t) {
if (null == t || "".equals(t)) {
return new JSONObject();
}
return (JSONObject) JSON.toJSON(t);
}
/**
* JSON 字符串转换为 HashMap
*
* @param json
* - String
* @return Map
*/
@SuppressWarnings("rawtypes")
public static Map toMap(String json) {
if (isEmpty(json)) {
return new HashMap();
}
return JSON.parseObject(json, HashMap.class);
}
/**
* 将map转化为string
*
* @param m
* @return
*/
@SuppressWarnings("rawtypes")
public static String toString(Map m) {
return JSONObject.toJSONString(m);
}
/**
* String转换为数组
*
* @param text
* @return
*/
public static <T> Object[] toArray(String text) {
return toArray(text, null);
}
/**
* String转换为数组
*
* @param text
* @return
*/
public static <T> Object[] toArray(String text, Class<T> clazz) {
return JSON.parseArray(text, clazz).toArray();
}
/**
* name1=value1&name2=value2格式的数据转换成json数据格式
*
* @param str
* @return
*/
public static JSONObject str2Json(String str) {
if (isEmpty(str)) {
return new JSONObject();
}
JSONObject json = new JSONObject();
String[] str1 = str.split("&");
String str3 = "", str4 = "";
if (null == str1 || str1.length == 0) {
return new JSONObject();
}
for (String str2 : str1) {
str3 = str2.substring(0, str2.lastIndexOf("="));
str4 = str2.substring(str2.lastIndexOf("=") + 1, str2.length());
json.put(str3, str4);
}
return json;
}
/**
* json数据格式 转换成name1=value1&name2=value2格式
*
* @param str
* @return
*/
@SuppressWarnings("rawtypes")
public static String json2Str(JSONObject json) {
if (isEmpty(json)) {
return null;
}
StringBuffer sb = new StringBuffer();
Iterator it = json.entrySet().iterator(); // 定义迭代器
while (it.hasNext()) {
Map.Entry er = (Entry) it.next();
sb.append(er.getKey());
sb.append("=");
sb.append(er.getValue());
sb.append("&");
}
sb.delete(sb.length() - 1, sb.length()); // 去掉最后的&
return sb.toString();
}
/**
* 将JDBC查询的数据转换成List类型
*
* @param ResultSet
* @return List
* @throws SQLException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List convertList(ResultSet rs) throws SQLException {
if (null == rs) {
return new ArrayList<>();
}
List list = new ArrayList();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
while (rs.next()) {
JSONObject rowData = new JSONObject();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
}
return list;
}
/**
* MD5加密
*
* @param message
* @return
*/
public static String md5Encode(String message) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(message.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字
// 如果生成数字未满32位,需要前面补0
int length = 32 - md5code.length();
for (int i = 0; i < length; i++) {
md5code = "0" + md5code;
}
return md5code;
}
/**
* base64 加密
*
* @param str
* @return
*/
public static String base64En(String str) {
Base64 base64 = new Base64();
byte[] encode = base64.encode(str.getBytes());
return new String(encode);
}
/**
* base64解密
*
* @param encodeStr
* @return
*/
@SuppressWarnings("static-access")
public static String base64De(String encodeStr) {
Base64 base64 = new Base64();
byte[] decodeStr = base64.decodeBase64(encodeStr);
return new String(decodeStr);
}
/**
* 十进制转二进制
*
* @param n
* @return
*/
public static String decToBinary(int n) {
String str = "";
while (n != 0) {
str = n % 2 + str;
n = n / 2;
}
return str;
}
/**
* 二进制转十进制
*
* @param n
* @return
*/
public static int binaryToDec(char[] cs) {
return binaryToDec(cs);
}
/**
* 二进制转十进制
*
* @param n
* @return
*/
public static int binaryToDec(String cs) {
return new BigInteger(new String(cs), 2).intValue();
}
/**
* 本方法的测试示例
*
* @param args
*/
@SuppressWarnings({ "rawtypes" })
public static void main(String[] args) {
/*
* String 和List 空数据判断
*/
String str1 = "";
String str2 = " ";
String str3 = null;
String str4 = "a";
List list = null;
List<String> list2 = new ArrayList<String>();
List<Object> list3 = new ArrayList<Object>();
list3.add("a");
System.out.println("str1 :" + isEmpty(str1)); // str1 :true
System.out.println("str2 :" + isEmpty(str2)); // str2 :true
System.out.println("str3 :" + isEmpty(str3)); // str3 :true
System.out.println("str4 :" + isEmpty(str4)); // str4 :false
System.out.println("list :" + isEmpty(list)); // list :true
System.out.println("list2 :" + isEmpty(list2)); // list2 :true
System.out.println("list3 :" + isEmpty(list3)); // list3 :false
/*
* 时间
*/
long start = getNowLongTime();
System.out.println("getNowTime():" + getNowTime()); // getNowTime():2017-09-26
// 17:46:44
System.out.println("getNowLongTime():" + getNowLongTime()); // getNowLongTime():1506419204920
System.out.println("getNowTime(sdfm):" + getNowTime(sdfm)); // getNowTime(sdfm):2017-09-26
// 17:46:44
// 920
System.out.println("当时时间向前推移30秒:" + changeTime(-30, sdf, "s")); // 2017-09-26
// 17:46:14
System.out.println("时间比较:" + isCompareDay(getNowTime(sdfm), changeTime(-30, sdf, "s"), "")); // 时间比较:false
System.out.println("getTNowTime():" + getTNowTime()); // getTNowTime():2017-09-26
// 17:46:44.921
System.out.println("LongTime2StringTime():" + longTime2StringTime(start, sd)); // LongTime2StringTime():20170926174644
/*
* 整型判断
*/
String st = "258369";
String st2 = "258369A!@";
String st3 = "258 369 ";
System.out.println("st:" + isInteger(st)); // st:true
System.out.println("st2:" + isInteger(st2)); // st2:false
System.out.println("st3:" + isInteger(st3)); // st3:false
/*
* 字符串反转
*/
String re = "abcdefg";
System.out.println("字符串反转:" + reverse(re)); // 字符串反转:gfedcba
/*
* 本机IP
*/
try {
System.out.println("本机IP:" + getLocalHostIp()); // 本机IP:192.168.1.111
} catch (UnknownHostException e) {
e.printStackTrace();
}
/*
* 随机数
*/
System.out.println("6位随机数:" + random(6)); // 6位随机数:222488
System.out.println("10位随机数:" + random2(10)); // 10位随机数:ZwW0pmofjW
/*
* JSON数据转换
*/
String value = "name1=value1&name2=value2&name3=value3";
JSONObject json = new JSONObject();
json.put("name1", "value1");
json.put("name2", "value2");
json.put("name3", "value3");
System.out.println("value:" + value); // value:name1=value1&name2=value2&name3=value3
System.out.println("str2Json:" + str2Json(value)); // str2Json:{"name1":"value1","name2":"value2","name3":"value3"}
System.out.println("json:" + json.toJSONString()); // json:{"name1":"value1","name2":"value2","name3":"value3"}
System.out.println("json2Str:" + json2Str(json)); // json2Str:name3=value3&name1=value1&name2=value2
String jsonString = json.toJSONString();
System.out.println("jsonString:" + jsonString); // {"name1":"value1","name2":"value2","name3":"value3"}
System.out.println("toJson(jsonString):" + toJson(jsonString)); // toJson(jsonString):{"name1":"value1","name2":"value2","name3":"value3"}
System.out.println("long TO String" + longTime2StringTime(32472115200L));
System.out.println("long TO String" + longTime2StringTime(1513330097L));
String time1 = "2018-04-04";
String time2 = "2018-04-04 14:48:00";
String time3 = "2018-04-04 14:48:00.000";
System.out.println("时间补全:" + complementTime(time1, sdfm, 1));
System.out.println("时间补全:" + complementTime(time2, sdfm, 2));
System.out.println("时间补全:" + complementTime(time3, sdfm, 1));
String time4 = addPlusMonths(time1, 2);
System.out.println("增加之前的数据:" + time1 + "增加月份之后的数据:" + time4);
System.out.println("相差月份:" + diffMonth("201711", "201801"));
int l = 2;
String string = "10101";
System.out.println(l + " 十进制转二进制: " + decToBinary(l));
System.out.println(string + " 二进制转十进制: " + binaryToDec(string));
}
}