package com.zzy.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class CharacterUtils {
/**
* 第一種方法,;length為產(chǎn)生的位數(shù)
*/
public static String getRandomString(int length)throws NumberFormatException{
//定義一個(gè)字符串(A-Z,a-z,,0-9)即62位,;
String str='zxcvbnmlkjhgfdsaqwertyuiopQWERTYU
IOPASDFGHJKLZXCVBNM1234567890';
//String str='1234567890';
//由Random生成隨機(jī)數(shù)
Random random=new Random();
StringBuffer sb=new StringBuffer();
//長(zhǎng)度為幾就循環(huán)幾次
for(int i=0; i<length; i){
//產(chǎn)生0-61的數(shù)字
int number=random.nextInt(62);
//將產(chǎn)生的數(shù)字通過length次承載到sb中
sb.append(str.charAt(number));
}
//將承載的字符轉(zhuǎn)換成字符串
return sb.toString();
}
/**
* 第二種方法
*/
public static String getRandomString2(int length){
//產(chǎn)生隨機(jī)數(shù)
Random random=new Random();
StringBuffer sb=new StringBuffer();
//循環(huán)length次
for(int i=0; i<length; i ){
//產(chǎn)生0-2個(gè)隨機(jī)數(shù),既與a-z,,A-Z,,0-9三種可能
int number=random.nextInt(3);
long result=0;
switch(number){
//如果number產(chǎn)生的是數(shù)字0;
case 0:
//產(chǎn)生A-Z的ASCII碼
result=Math.round(Math.random()*25 65);
//將ASCII碼轉(zhuǎn)換成字符
sb.append(String.valueOf((char)result));
break;
case 1:
//產(chǎn)生a-z的ASCII碼
result=Math.round(Math.random()*25 97);
sb.append(String.valueOf((char)result));
break;
case 2:
//產(chǎn)生0-9的數(shù)字
sb.append(String.valueOf
(new Random().nextInt(10)));
break;
}
}
return sb.toString();
}
public static void main(String[] args) {
// Long lon=new Long(Long.parseLong(getRandomString(4)));
//System.out.println('>>>>>>>' lon);
for(int i=0; i<5000000; i){
Date date=new Date();
String sf = new SimpleDateFormat
('yyMMddhhmmss').format(date);
String strid2=sf getRandomString(4);
// System.out.println(' ' strid2);
long id=Long.parseLong(strid2);
System.out.println('>>>>>>>' id);
}
// String i='1234567890134456';
// long id=Long.parseLong(i);
}
}21套精品Java架構(gòu)師高并發(fā)高性能高可用分布式集群電商緩存性能調(diào)優(yōu)設(shè)計(jì)項(xiàng)目實(shí)戰(zhàn)視教程
4000G java 架構(gòu)師進(jìn)階視頻資源下載: