1 package com.foxinmy.weixin4j.util;
2
3 import java.util.Random;
4 import java.util.UUID;
5
6
7
8
9
10
11
12
13
14
15 public class RandomUtil {
16
17 private static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
18 private static final String LETTERCHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
19 private static final String NUMBERCHAR = "0123456789";
20
21
22
23
24
25
26
27
28 public static String generateString(int length) {
29 StringBuilder sb = new StringBuilder(length);
30 Random random = new Random();
31 for (int i = 0; i < length; i++) {
32 sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
33 }
34 return sb.toString();
35 }
36
37
38
39
40
41
42
43
44 public static String generateStringByNumberChar(int length) {
45 StringBuilder sb = new StringBuilder(length);
46 Random random = new Random();
47 for (int i = 0; i < length; i++) {
48 sb.append(NUMBERCHAR.charAt(random.nextInt(NUMBERCHAR.length())));
49 }
50 return sb.toString();
51 }
52
53
54
55
56
57
58
59
60 public static String generateStringByLetterCharr(int length) {
61 StringBuilder sb = new StringBuilder(length);
62 Random random = new Random();
63 for (int i = 0; i < length; i++) {
64 sb.append(LETTERCHAR.charAt(random.nextInt(LETTERCHAR.length())));
65 }
66 return sb.toString();
67 }
68
69
70
71
72
73
74
75
76 public static String generateLowerString(int length) {
77 return generateStringByLetterCharr(length).toLowerCase();
78 }
79
80
81
82
83
84
85
86
87 public static String generateUpperString(int length) {
88 return generateStringByLetterCharr(length).toUpperCase();
89 }
90
91
92
93
94
95
96 public static String getUUID() {
97 String uuid = UUID.randomUUID().toString();
98 return uuid.substring(0, 8) + uuid.substring(9, 13)
99 + uuid.substring(14, 18) + uuid.substring(19, 23)
100 + uuid.substring(24);
101 }
102
103 public static void main(String[] args) {
104 System.out.println(System.nanoTime());
105 System.out.println(System.currentTimeMillis());
106 }
107 }