`
Apple_2011
  • 浏览: 37019 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

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

    博客分类:
  • 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);
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics