lundi 2 mars 2015

Remove random decimal points from randomly generated timestamps

I've a code for generating random timestamps. However I came to find that when I generate a random timestamp, it contains some decimal points to te seconds field. Does anyone know what is wrong here


Code



long rangeBegin = Timestamp.valueOf("2015-01-01 00:00:00").getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentDate = new Date();
String currentDateString = simpleDateFormat.format(currentDate);
long rangeEnd = Timestamp.valueOf(currentDateString).getTime();
long diff = rangeEnd - rangeBegin + 1;
Timestamp randomTimestamp = new Timestamp(rangeBegin + (long)(Math.random() * diff));


Sample output


randomTimestamp = 2015-02-20 02:36:00.646


Thanks and sorry for poor English.





Aucun commentaire:

Enregistrer un commentaire