0

I got a question regarding the random generated date. So what I am trying to do is I random generate a day between 1-30, month between 1-12 and year by default is 2016.

Random rand = new Random();
int date = 1 + rand.nextInt((30 - 1) + 1);
int month = 1 + rand.nextInt((12 - 1) + 1);
if(month == 1){
    monthStr = "January";
}else if(month == 2){
    monthStr = "February";
}else if(month == 3){
    monthStr = "March";
}else if(month == 4){
    monthStr = "April";
}else if(month == 5){
    monthStr = "May";
}else if(month == 6){
    monthStr = "June";
}else if(month == 7){
    monthStr = "July";
}else if(month == 8){
    monthStr = "August";
}else if(month == 9){
    monthStr = "September";
}else if(month == 10){
    monthStr = "October";
}else if(month == 11){
    monthStr = "November";
}else if(month == 12){
    monthStr = "December";
}

I used a for loop to random generate the date for 100 times. Then, I append the string together to store it into a string array list.

ArrayList<String> dateTimeList = new ArrayList<String>();
dateTimeList.add(date + " " +  monthStr + " 2016");

After that, I pass the entire list into another method. In this method, I am trying to generate again random date but in this case, I need to generate a date which is after the above one.

In another word, the first time when I random generated 100 dates is for the filing date. Then I pass in the entire list into another method, inside that method, I need to generate another 100 dates which is after each filing date.

Any ideas how could I achieve this? I know there is some other way to generate a random date but I preferred to be in this way as I will be using it in some other places.

Thanks in advance.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
QWERTY
  • 2,303
  • 9
  • 44
  • 85
  • Hmm because I will be inserting those dates into database and it works in specific format such as 25 January 2016. That's why I am doing it in that way – QWERTY Oct 20 '16 at 13:52
  • 1
    You could convert the date into milliseconds, then pass the millisections as the minimum to the random function. The you interpret the number you get from the random as a date – Erik Oct 20 '16 at 13:53
  • @DimaSan Excuse me, I think it is not duplicate of thread. What that thread asking is to generate random dates without any condition. But what I asked is how to generate a date which is after certain dates. – QWERTY Oct 20 '16 at 13:54
  • What about the 31th day in several months in your solution? And what about the 30th february? Using the default DateTime class saves you a lot of trouble... – Philip W Oct 20 '16 at 13:55
  • Okay maybe you're right. But what is preventing you to add this your condition to the working solution? – DimaSan Oct 20 '16 at 13:56
  • the 100 first dates must all be before the 100 next dates, right ? Do you allow duplicate dates? – Nicolas Filotto Oct 20 '16 at 13:59
  • Yeah. The first 100 is something like filing date. Another 100 is like the processing date. Yeah duplication is allowed. – QWERTY Oct 20 '16 at 14:01

2 Answers2

3

I assume you're using the latest Java with new Date API.

First create the earliest acceptable date, for example:

LocalDate baseDate = LocalDate.now(); //use today, OR
LocalDate baseDate = LocalDate.of(2016, 11, 30); //use any date - you can randomize the year/month/date if you want

Next, select a random number. Note, that you can add days or months or years to your date - this number should represent one of those. Let's assume days.

Integer maximumRandomValue = 100; //we want to add a number of days between 0-100
Integer randomDays = (int)(maximumRandomValue*Math.random());

Now just add them to your base date:

LocalDate randomDate = baseDate.plusDays(randomDays);

Also, if you want to convert it to String (although your database code should be able to handle that for you) here is a simple way (using the format as you provided in question)

String dateAsString = DateTimeFormatter.ofPattern("d MMMM yyyy").format(randomDate);

For more options on the format, please refer to API docs of DateTimeFormatter.

jderda
  • 890
  • 6
  • 18
  • But what is the format of the date? is it something like 20 October 2016? – QWERTY Oct 20 '16 at 14:06
  • The LocalDate object does not have a specific format - it is just Date-related information. I just added an example how to format the date in the way you want it - basically use a DateTimeFormatter class. It allows you to format it in any way you want. – jderda Oct 20 '16 at 14:09
  • Sorry but is there any way to twist it so that it works in this way: first I random generate 100 dates, then I random generate another 100 dates which is after the first 100. Cause from what I understand of your code is you random generate 100 dates after today's date. – QWERTY Oct 20 '16 at 14:11
  • Sure, just replace the first line with something like: int year = 2015; int month = 10; int day = 8; LocalDate baseDate = LocalDate.of(year, month, day); Of course you can randomize those year/month/day values as well. Updated the answer to include this – jderda Oct 20 '16 at 14:16
  • Thanks so much! It works! – QWERTY Oct 20 '16 at 14:27
2

You can convert date into milliseconds and then have an big integer generated using random function. Add this random numbe to milliseconds and convert it back to date.

Here how it is =>

import java.math.BigInteger;
import java.util.Date;
import java.util.Random;

    public class generateRandomDate {

        public static void generateRandomDate(){

            Date d = new Date();
            System.out.println("Input Date ==> "+ d);
            System.out.println("Input Milliseconds ==> "+ d.getTime());
            BigInteger randomBigInteger = nextRandomBigInteger(BigInteger.valueOf(d.getTime()));
            System.out.println("RandomBinInteger ==> "+ randomBigInteger);
            System.out.println("Next random Date ==> "+ new Date(d.getTime() + randomBigInteger.longValue()));
        }

        public static BigInteger nextRandomBigInteger(BigInteger n) {
            Random rand = new Random();
            BigInteger result = new BigInteger(n.bitLength(), rand);
            while( result.compareTo(n) >= 0 ) {
                result = new BigInteger(n.bitLength(), rand);
            }
            return result;
        }
        public static void main(String[] args) {
            generateRandomDate();
        }


    }
mhasan
  • 3,703
  • 1
  • 18
  • 37
Khuzi
  • 2,792
  • 3
  • 15
  • 26
  • Hmm do you mind to show me with some examples? – QWERTY Oct 20 '16 at 13:55
  • Updated the answer with code snippet. You can control the random big integer generation with input to the function. I have passed long value of current date to random generator for now. Hope it helps you! – Khuzi Oct 20 '16 at 14:14