public static String generatePassword(int length) {
String result = null;
result = getRandomPassword(length);
if (result.matches(".*[a-z]{1,}.*") && result.matches(".*[A-Z]{1,}.*") && result.matches(".*[0-9]{1,}.*") && result.matches(".*[~!@#$%^&*\\.?]{1,}.*")) {
return result;
}
return generatePassword(length);
}
public static String getRandomPassword(int length){
char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*.?".toCharArray();
StringBuilder sb = new StringBuilder();
Random r = new Random();
for (int x = 0; x < length; ++x) {
sb.append(charr[r.nextInt(charr.length)]);
}
return sb.toString();
}