How to create random timestamp in java?

I want to create a random timestamp and add a random increment to it to create a second timestamp. perhaps?

If I pass in random long values ​​to create a timestamp, and I want to randomly generate this long value, what would be the limitations for generating this value to give a timestamp in 2012, for example?

+13
java timestamp random
Jun 13 2018-12-12T00:
source share
10 answers

You need to scale the random number to be in the range of a certain year, and add the year starting as an offset. The number of milliseconds per year changes from one year to another (leap years have an extra day, certain years are minutes of a jump, etc.). Therefore, you can determine the range before scaling as follows:

long offset = Timestamp.valueOf("2012-01-01 00:00:00").getTime(); long end = Timestamp.valueOf("2013-01-01 00:00:00").getTime(); long diff = end - offset + 1; Timestamp rand = new Timestamp(offset + (long)(Math.random() * diff)); 
+36
Jun 13 2018-12-12T00:
source share
β€” -

For your example, the long value for passing in Date should be between 1325397600 and 1293861599 for 2012. Use this site to check! To create a random date, you can do something like this:

 Random r =new Random(); long unixtime=(long) (1293861599+r.nextDouble()*60*60*24*365); Date d = new Date(unixtime); 
+6
Jun 13 '12 at 13:56
source share

Use ApacheCommonUtils to generate a random long within a given range, and then create a Date from that long.

Example:

 import org.apache.commons.math.random.RandomData; import org.apache.commons.math.random.RandomDataImpl; public Date nextDate(Date min, Date max) { RandomData randomData = new RandomDataImpl(); return new Date(randomData.nextLong(min.getTime(), max.getTime())); } 
+2
Jun 15 '13 at 20:24
source share

You can create a random timestamp by creating a random long in the appropriate range, and then treating it as a millisecond precision timestamp; e.g. new Date(long) .

To define a range, create a Date or Calendar object (or similar) that represents the start date and end date of the range, and call long getTime() or the equivalent to get millisecond time values. Then create a random long in this range.

+1
Jun 13 2018-12-12T00:
source share

IMO, the best time library in java is JodaTime.

If you want a random TimeStam in 2012, for example, you should start by creating a date 01/01/2012, and then add a random amount of time. Finally, create a TimeStamp object with a long constructor:

 org.joda.time.DateTime tempDateTime = org.joda.time.DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime("2012-01-01").plusMillis(my_random_value); return new Timestamp(tempDateTime .getMillis()) 
+1
Jun 13 2018-12-12T00:
source share

Start by finding out the beginning of 2012:

  long start2012 = new SimpleDateFormat("yyyy").parse("2012").getTime(); 

Get a random millisecond for a year:

  final long millisInYear2012 = 1000 * 60 * 60 * 24 * 365 + 1000; // Have to account for the leap second! long millis = Math.round(millisInYear2012 * Math.random()); Timestamp timeStamp = new Timestamp(start2012 + millis); 
+1
Jun 13 2018-12-12T00:
source share

The following code generates a random timestamp during 2012.

  DateFormat dateFormat = new SimpleDateFormat("yyyy"); Date dateFrom = dateFormat.parse("2012"); long timestampFrom = dateFrom.getTime(); Date dateTo = dateFormat.parse("2013"); long timestampTo = dateTo.getTime(); Random random = new Random(); long timeRange = timestampTo - timestampFrom; long randomTimestamp = timestampFrom + (long) (random.nextDouble() * timeRange); 
0
Jun 13 2018-12-12T00:
source share

Check out this method:

 public static Timestamp dateRandom(int initialYear, int lastYear) { if (initialYear > lastYear) { int year = lastYear; lastYear = initialYear; initialYear = year; } Calendar cInitialYear = Calendar.getInstance(); cInitialYear.set(Calendar.YEAR, 2015); long offset = cInitialYear.getTimeInMillis(); Calendar cLastYear = Calendar.getInstance(); cLastYear.set(Calendar.YEAR, 2016); long end = cLastYear.getTimeInMillis(); long diff = end - offset + 1; return new Timestamp(offset + (long) (Math.random() * diff)); } 
0
Dec 08 '16 at 18:55
source share

Another way

 public static Timestamp getRandomTime(){ Random r = new Random(); int Low = 100; int High = 1500; int Result = r.nextInt(High-Low) + Low; int ResultSec = r.nextInt(High-Low) + Low; Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MINUTE, - Result); calendar.add(Calendar.SECOND, - ResultSec); java.sql.Timestamp ts = new java.sql.Timestamp(calendar.getTimeInMillis()); return ts; } 
0
Jan 03 '17 at 21:15
source share
 import org.joda.time.*; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; DateTime d1 =DateTime.now().withZone(DateTimeZone.UTC); DateTime d0 = d1.minusSeconds(20); DateTime d2 = d1.plusSeconds(20); Random r = new Random(); long t1 = d0.getMillis(); long t2 = d2.getMillis(); //DateTime d1 = new DateTime(t1); //DateTime d2 = new DateTime(t2); Random random = new Random(); long rand = t1 + (long) (random.nextDouble() * (t2-t1)); DateTime randDatetime = new DateTime(rand); String datestr= randDatetime.toString("MM/dd/YYYY hh:mm:ss") ; System.out.println(datestr) ; 
0
Oct 27 '17 at 7:41
source share



All Articles