Generating Random Date time in java (joda time)

后端 未结 5 1579
心在旅途
心在旅途 2021-01-04 21:32

Is it possible to generate a random datetime using Jodatime such that the datetime has the format yyyy-MM-dd HH:MM:SS and it should be able to generate two random datetimes

5条回答
  •  遥遥无期
    2021-01-04 22:05

    Simple

    long rangebegin = Timestamp.valueOf("2013-02-08 00:00:00").getTime();
    long rangeend = Timestamp.valueOf("2013-02-08 00:58:00").getTime();
    long diff = rangeend - rangebegin + 1;
    Timestamp rand = new Timestamp(rangebegin + (long)(Math.random() * diff));
    

提交回复
热议问题