package com.test.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class CharacterUtils {
	
	
	/**
	 * 第一种方法;length为产生的位数
	 */
	public static String  getRandomString(int length)throws NumberFormatException{
		//定义一个字符串(A-Z,a-z,0-9)即62位;
		String str="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
		//String str="1234567890";
		//由Random生成随机数
		Random  random=new Random();	
		
		StringBuffer sb=new StringBuffer();
		//长度为几就循环几次
		for(int i=0; i<length; ++i){
			//产生0-61的数字
			int number=random.nextInt(62);
			//将产生的数字通过length次承载到sb中
			sb.append(str.charAt(number));
		}
		//将承载的字符转换成字符串
		return sb.toString();
	}

	/**
	 * 第二种方法
	 */
	public static String getRandomString2(int length){
		//产生随机数
		Random random=new Random();
		StringBuffer sb=new StringBuffer();
		//循环length次
		for(int i=0; i<length; i++){
			//产生0-2个随机数,既与a-z,A-Z,0-9三种可能
			int number=random.nextInt(3);
			long result=0;
			
			switch(number){
			//如果number产生的是数字0;
			case 0:
				//产生A-Z的ASCII码
				result=Math.round(Math.random()*25+65);
				//将ASCII码转换成字符
				sb.append(String.valueOf((char)result));
				break;
				case 1:
					//产生a-z的ASCII码
					result=Math.round(Math.random()*25+97);
					sb.append(String.valueOf((char)result));
				break;
				case 2:
					//产生0-9的数字
					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);
	

}

}
最近下载更多
mmzsans  LV1 2023年5月6日
史蒂夫FF  LV1 2023年4月2日
1361148450  LV1 2023年2月21日
wuziayng1232  LV10 2023年2月21日
djfdshfjakl  LV1 2022年11月3日
最代码-宋家辉  LV61 2022年6月9日
daxianger  LV4 2020年4月11日
ovohei  LV2 2019年11月5日
wyy_baseone  LV2 2019年10月7日
SaDanC  LV2 2019年6月8日
最近浏览更多
mmzsans  LV1 2023年5月6日
史蒂夫FF  LV1 2023年4月2日
1361148450  LV1 2023年2月21日
wuziayng1232  LV10 2023年2月21日
jk-mack  LV5 2023年2月16日
jjfskldjf  LV2 2022年11月27日
djfdshfjakl  LV1 2022年11月3日
ljt289917726  LV3 2022年9月5日
最代码-宋家辉  LV61 2022年6月9日
挖金矿 2022年5月14日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友