moombaz
asked on
how to create 6 digit random number pin - JAVA
1) i need to create a 6 digit pin. So need a random number generator.
2) how to i get a timestamp & to use the timestamp to see if it exceed 15seconds ??
2) how to i get a timestamp & to use the timestamp to see if it exceed 15seconds ??
1.
2. Do you mean you want the current timestamp and what does it compare to for "exceeds 15 seconds"?
java.util.Random generator = new java.util.Random();
generator.setSeed(System.currentTimeMillis());
int i = generator.nextInt(1000000) % 1000000;
java.text.DecimalFormat f = new java.text.DecimalFormat("000000");
System.out.println(f.format(i));
2. Do you mean you want the current timestamp and what does it compare to for "exceeds 15 seconds"?
ASKER
yes.. my requirement is to get the time generated when the pin is generated then next time if anyone were to use the pin, i need to check if the pin generated exceeds 15 secs already.
my idea/logic is that have a pin put and tie it to a timestamp together with userid and password and write to a text file.
eg. testuser:p4ssword:806390:t imestamp
afterwhich, another java program read the file and get the timestamp and compare with the current timestamp and see if it has already exceeded 15secs. if exceeds, it will print out "Pin expired!"
About the reading and splitting the string within the text file, i know how to do it. just need to know how to do the random number and compare timestring.
my idea/logic is that have a pin put and tie it to a timestamp together with userid and password and write to a text file.
eg. testuser:p4ssword:806390:t
afterwhich, another java program read the file and get the timestamp and compare with the current timestamp and see if it has already exceeded 15secs. if exceeds, it will print out "Pin expired!"
About the reading and splitting the string within the text file, i know how to do it. just need to know how to do the random number and compare timestring.
1) int n1 = Integer.parseInt(("" + Long.parseLong(("" + Math.random()).substring(2 ))).substr ing(0, 6));
2) Question is not clear, can you please clarify what is meant by "it exceed 15 seconds"; question is, exceed what by 15 seconds?
2) Question is not clear, can you please clarify what is meant by "it exceed 15 seconds"; question is, exceed what by 15 seconds?
To save the value when the PIN is generated 1st time, you can save the long value corresponding to the time:
Long beginTine = (new java.util.Date()).getTime( );
You can save this in the password file according to the format you have shown; later on, you can find the new log value again by using the same statement as above and find the difference between the two long values (the saved one and the current one) to find the time difference in milliseconds.
Long beginTine = (new java.util.Date()).getTime(
You can save this in the password file according to the format you have shown; later on, you can find the new log value again by using the same statement as above and find the difference between the two long values (the saved one and the current one) to find the time difference in milliseconds.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I assume you format the timestamp like this:
yyyyMMddHHmmssS
Please refer to the following for details:
http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
Then to get current timestamp, you may:
yyyyMMddHHmmssS
Please refer to the following for details:
http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
Then to get current timestamp, you may:
Calendar calendar = Calendar.getInstance();
java.util.Date now = calendar.getTime();
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmssS");
// to format the timestamp to your specific format
String s = df.format(now);
// to parse the read-in timestamp (I assume you know how to read in from text file and how to separate your string into tokens)
try {
java.util.Date rd = df.parse("<read-in timestamp>");
Calendar c = Calendar.getInstance();
c.setTime(rd);
c.add(Calendar.SECOND, 15);
if (now.compareTo(rd) > 0) {
// after 15 second: expires!
}
} catch (Exception e) {
// do
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
-> http://java.about.com/od/javautil/a/randomnumbers.htm
Also if you need "really" random numbers, not pseudo random generated, you can use
-> http://www.random.org
-> http://www.random.org/integers/?num=6&min=0&max=9&col=5&base=10&format=html&rnd=new