首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java生成一个字母加数目字的随机数

2012-11-11 
java生成一个字母加数字的随机数//获取一个字母加数字的随机字符串:public String getCharacterAndNumber(

java生成一个字母加数字的随机数
//获取一个字母加数字的随机字符串:
public String getCharacterAndNumber(int length) {  
String password = "";  
    Random random = new Random();  
    for(int i = 0; i < length; i++) {  
    String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 输出字母还是数字  
        if("char".equalsIgnoreCase(charOrNum)){ // 字符串  
            int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; //取得大写字母还是小写字母  
            password += (char) (choice + random.nextInt(26));  
        }else if("num".equalsIgnoreCase(charOrNum)) {  // 数字  
        password += String.valueOf(random.nextInt(10));  
        }
     }
    return password;  

生成个年月日时分秒yyMMddhhmmss:
long no=Long.parseLong(getDateTime("yyMMddhhmmss", new Date()));

public static final String getDateTime(String aMask, Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate == null) {
            log.error("aDate is null!");
        } else {
            df = new SimpleDateFormat(aMask);
            returnValue = df.format(aDate);
        }

        return (returnValue);
}
去掉一个数组中相同的字符串:
public static void main(String[] args) {
String str="123,456,456,789,123,100";
System.out.println(str+"------");
String ss[]=str.split(",");
Set<String> set=new HashSet<String>();
StringBuffer sb=new StringBuffer();
for (int i = 0; i < ss.length; i++) {
set.add(ss[i]);
}
for (String s : set) {
sb.append(s+",");
}
System.out.print(sb);
}

热点排行