mercredi 22 novembre 2017

Java: Generate a random date between a range (range starting from current date/time to a random future date (e.g 5-10 days from current date/time)

Java beginner here. After googling and researching, this is the best way I found in my opinion to generate the current date/time:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();

  1. How do I place the above current date/time into a variable?
  2. How do I generate a RANDOM future date from current date/time (for example, the random range can be from 5 - 10 days), this means I do not have a fixed date for the future date.
  3. How do I store that future date into a variable?

SIDE NOTE: Why I asked Question 1 and 3, it's because I can use the variables storing both dates for comparison and evaluation purposes (used in if-else blocks)

Thanks a lot for your help!




Aucun commentaire:

Enregistrer un commentaire