R
R
roman38472014-04-26 13:53:52
Java
roman3847, 2014-04-26 13:53:52

Whether it is possible, working in Random generating date to receive values ​​of a look 03, 07, instead of 3, 7?

sw = new FileWriter(file, true);
sw.write((int) (1 + Math.random() * 30) +"." + (int) (1 + Math.random() * 12) + "." + (int) (1970 + Math.random() * 40));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2014-04-26
@roman3847

You can generate random integers from the range [101..131], and then cut off the first character after casting to the string.
And in your case, it seems to me that it would be most correct to generate a single integer from the sequence of the range [from January 1, 1970 to December 31, 2009], and then convert it to a date and format it as needed - otherwise you will have "February 30" , "June 31" and many other exciting dates.

L
leventov, 2014-04-26
@leventov

Read about string formatting .

sw = new FileWriter(file, true);
sw.write(String.format("%02d.%02d.%d", (int) (1 + Math.random() * 30), (int) (1 + Math.random() * 12), (int) (1970 + Math.random() * 40)));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question