标签:
1 public static synchronized String generateUniqueID() { 2 if (seq > ROTATION) 3 seq = 0; 4 buf.delete(0, buf.length()); 5 date.setTime(System.currentTimeMillis()); 6 // 年月日时分秒 + 自增的五位十进制数 7 String str = String.format("%1$tY%1$tm%1$td%1$tk%1$tM%1$tS%2$05d", 8 date, seq++); 9 return str; 10 }
1 public class PathUtil { 2 3 private URL ROOT_URL = ClassLoader.getSystemResource(""); 4 private String ROOT_PATH; 5 private String TEMP_PATH; 6 private String CONFIG_PATH; 7 8 private static PathUtil pathUtil; 9 10 public PathUtil(){ 11 if (ROOT_URL == null) { 12 ROOT_PATH = new File("").getAbsolutePath(); 13 TEMP_PATH = ROOT_URL + "tmp"; 14 } else { 15 ROOT_PATH = ROOT_URL.getFile(); 16 TEMP_PATH = ROOT_URL.getFile() + "/tmp"; 17 } 18 CONFIG_PATH = ROOT_PATH + "/resource/configuration/"; 19 } 20 21 public static PathUtil getInstance(){ 22 if(pathUtil == null){ 23 pathUtil = new PathUtil() ; 24 } 25 return pathUtil ; 26 } 27 28 public URL getRootUrl() { 29 return ROOT_URL; 30 } 31 32 public String getRootPath() { 33 return ROOT_PATH; 34 } 35 36 public String getConfigPath() { 37 return this.CONFIG_PATH; 38 } 39 40 public String getTempPath() { 41 return TEMP_PATH; 42 } 43 }
UUID.randomUUID()是JDK提供的一个自动生成主键的方法;
UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的;
它是由一个十六位的数字组成,格式为:当前日期和时间(UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则第一个部分不同,其余相同)+时钟序列+全局唯一的IEEE机器识别号(如果有网卡,从网卡获得,没有网卡以其他方式获得),UUID的唯一缺陷在于生成的结果串会比较长。
例如:
1 UUID uuid = UUID.randomUUID(); 2 System.out.println(uuid.toString()); 3 // 输出:04c8f5c4-31c3-4fd0-bfa3-96a364fff404 4 // 输出:acdc2ac5-fa3b-4b4b-b0ec-9e76d6efa337 5 // 输出:f397681d-c75c-469c-beeb-8525174ed23f
1 public class RandomStringUtil { 2 3 public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 4 public static final String letterChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 5 public static final String numberChar = "0123456789"; 6 7 private static RandomStringUtil randomStringUtil; 8 9 public RandomStringUtil(){} 10 11 public static RandomStringUtil getInstance(){ 12 if(randomStringUtil == null){ 13 randomStringUtil = new RandomStringUtil() ; 14 } 15 return randomStringUtil ; 16 } 17 18 /** 19 * 生成包含大小写字母+数字的随机字符串 20 * @param length 字符串长度 21 * @return 随机字符串 22 */ 23 public static String generateAllCharRandomly(int length) { 24 StringBuilder sBuilder = new StringBuilder(); 25 Random random = new Random(); 26 for (int i = 0; i < length; i++) { 27 sBuilder.append(allChar.charAt(random.nextInt(allChar.length()))); 28 } 29 return sBuilder.toString(); 30 } 31 32 /** 33 * 生成只包含大小写字母的随机字符串 34 * @param length 字符串长度 35 * @return 随机字符串 36 */ 37 public static String generateLetterCharRandomly(int length) { 38 StringBuilder sBuilder = new StringBuilder(); 39 Random random = new Random(); 40 for (int i = 0; i < length; i++) { 41 sBuilder.append(letterChar.charAt(random.nextInt(letterChar.length()))); 42 } 43 return sBuilder.toString(); 44 } 45 46 /** 47 * 生成只包含数字的随机字符串 48 * @param length 字符串长度 49 * @return 随机字符串 50 */ 51 public static String generateNumberRandomly(int length) { 52 StringBuilder sBuilder = new StringBuilder(); 53 Random random = new Random(); 54 for (int i = 0; i < length; i++) { 55 sBuilder.append(numberChar.charAt(random.nextInt(numberChar.length()))); 56 } 57 return sBuilder.toString(); 58 } 59 60 public static void main(String[] args) { 61 for(int i=0; i<10; i++) { 62 System.out.println(generateNumberRandomly(20)); 63 } 64 } 65 }
1 /** 2 * 利用序列化实现对象的深拷贝 3 * 或直接使用org.apache.commons.lang.SerializationUtils这个工具类即可 4 */ 5 public class CloneUtil { 6 7 // 克隆一个对象 8 @SuppressWarnings("unchecked") 9 public static <T extends Serializable> T clone(T obj) { 10 // 克隆产生的对象 11 T clonedObj = null; 12 try { 13 // 读取对象字节数据 14 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 15 ObjectOutputStream oos = new ObjectOutputStream(baos); 16 oos.writeObject(obj); 17 oos.close(); 18 // 分配内存空间,写入原始对象,生成新对象 19 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 20 ObjectInputStream ois = new ObjectInputStream(bais); 21 //返回新对象,并做类型转换 22 clonedObj = (T)ois.readObject(); 23 ois.close(); 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 return clonedObj; 28 } 29 }
提供操纵日期和时间各组成部分的方法。必须将Date对象转换为字符串,才能将其输出。
1 Date d = new Date(); 2 System.out.println(d);// 获得一个日期的字符串 3 System.out.println(d.getDate());// 获得日期中的日 4 System.out.println(d.getDay());// 获得日期中的星期几 5 System.out.println(d.getHours());// 获得小时 6 System.out.println(d.getMinutes());// 获得分 7 System.out.println(d.getMonth());// 获得月份,比实际月份要 8 System.out.println(d.getSeconds());// 获得秒 9 System.out.println(d.getTime());// 获得自1970年1月1日起以毫秒为单位的时间 10 System.out.println(d.getYear());// 获得年 11 System.out.println(d.toLocaleString());// 从日期字符串中截取时间
根据给定的Date对象,可以以整型的形式检索信息。它是抽象类,因此无法像Date类一样实例化
1 Calendar calendar = Calendar.getInstance(); 2 System.out.println(calendar.get(calendar.YEAR)); 3 System.out.println(calendar.get(calendar.MINUTE)); 4 System.out.println(calendar.get(calendar.DAY_OF_YEAR)); // 返回这一天在这一年中是第几天 5 calendar.clear(); 6 System.out.println(calendar.get(calendar.YEAR)); 7 Date today = new Date(); 8 // copy日期:java:Date-》sql:Date 9 Date oneDate = new Date(today.getTime());
1 // 日期格式化类 2 Date today = new Date(); 3 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); 4 System.out.println("没有转换:" + today.toLocaleString()); 5 System.out.println("1970-1-1到至今毫秒:" + today.getTime()); 6 System.out.println("转换:" + sdf.format(today)); // 将Date转换成指定格式的字符串 7 try { 8 Date date2 = sdf.parse("2001年12月1 23:45:23"); // 将字符串转换成Date 9 } catch (ParseException e) { 10 e.printStackTrace(); 11 }
1 public static final String encoderPwdByMd5(String str) { 2 String newStr = null; 3 try { 4 MessageDigest md5 = MessageDigest.getInstance("MD5"); 5 BASE64Encoder base64en = new BASE64Encoder(); 6 // 加密后的字符串 7 newStr = base64en.encode(md5.digest(str.getBytes("utf-8"))); 8 } catch (NoSuchAlgorithmException e1) { 9 e1.printStackTrace(); 10 } catch (UnsupportedEncodingException e2) { 11 e2.printStackTrace(); 12 } 13 return newStr; 14 }
1 import java.util.Random; 2 3 public class GenerateUtil { 4 5 /** 6 * 概率数组抽取 7 * 8 * @param array 9 * 概率数组(和为1) 10 * @return 下标 11 */ 12 public static int getRandomArray(double[] array) { 13 double random = Math.random(); 14 double rate = 0; 15 for (int i = 0; i < array.length; i++) { 16 rate += array[i]; 17 if (random <= rate) { 18 return i; 19 } 20 } 21 return 0; 22 } 23 24 /** 25 * 概率数组抽取 26 * 27 * @param array 28 * 概率数组(和为1) 29 * @return 下标 30 */ 31 public static int getRandomArray(Double[] array) { 32 double random = Math.random(); 33 double rate = 0; 34 for (int i = 0; i < array.length; i++) { 35 rate += array[i]; 36 if (random <= rate) { 37 return i; 38 } 39 } 40 return 0; 41 } 42 43 /** 44 * 生成n个small-big的不重复随机数字 45 * 46 * @param n 47 * 随机数字个数 48 * @param n 49 * 随机数字个数 50 * @return 随机数字数组 51 */ 52 public static int[] generateDifNums(int n, int small, int big) { 53 54 int length = big - small + 1; 55 int[] seed = new int[length]; 56 for (int i = 0; i < length; i++) { 57 seed[i] = small + i; 58 } 59 int[] ranArr = new int[n]; 60 Random ran = new Random(); 61 for (int i = 0; i < n; i++) { 62 int j = ran.nextInt(length - i); 63 ranArr[i] = seed[j]; 64 seed[j] = seed[length - 1 - i]; 65 66 } 67 return ranArr; 68 } 69 70 /** 71 * 概率整数抽取 72 * 73 * @param min 74 * @param max 75 * @return 范围整数 76 */ 77 public static int getRandomInteger(int min, int max) { 78 return (int) ((max - min + 1) * Math.random() + min); 79 } 80 81 /** 82 * 根据array中值的比重进行抽取 83 * 84 * @param array 85 * @param count 86 * @return 返回对应下标数组 87 */ 88 public static Integer[] getRandomArray(Integer[] array, int count) { 89 if (count > array.length || count <= 0) { 90 return array; 91 } 92 Integer result[] = new Integer[count]; 93 for (int i = 0; i < count; i++) { 94 int r = 0; 95 for (int j = 0; j < array.length; j++) { 96 r = r + array[j]; 97 } 98 int random = new Random().nextInt(r); 99 int rate = 0; 100 for (int k = 0; k < array.length; k++) { 101 if (array[k] > 0) { 102 rate += array[k]; 103 if (random <= rate) { 104 array[k] = 0; 105 result[i] = k; 106 break; 107 } 108 } 109 } 110 111 } 112 return result; 113 } 114 }
标签:
原文地址:http://www.cnblogs.com/xianDan/p/4292841.html