0

I have:

OffsetDateTime earliestStartTime = 20170401T000000Z
OffsetDateTime latestStartTime = 20170531T235959Z

how can I generate a time range such that:

startTime is a random OffsetDateTime in [earliestStartTime .. latestStartTime];
Duration is a random int in [1 .. 90], representing number of Days.
endTime is startTime + Duration.

I do not have "com.pholser.junit.quickcheck.random.SourceOfRandomness", so SourceOfRandomness cannot be used.

Thanks in advance!

leo
  • 357
  • 2
  • 15

1 Answers1

0

You can solve this in two steps:

  • Compute the difference between the latest and the earliest start time. Compute a random value between 0 and this difference, and add it to the earliest start time to obtain a random start time
  • Create a Duration from a random number of days, where this number is between 0 and 90 in your case

(Of course, adding this duration to the start time will yield the end time)

Here is an example:

import java.time.Duration;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Random;

public class RandomOffsetDateTimeRange
{
    public static void main(String[] args)
    {
        OffsetDateTime earliestStartTime = 
            OffsetDateTime.parse("2017-04-01T00:00:00+00:00");
        OffsetDateTime latestStartTime = 
            OffsetDateTime.parse("2017-05-31T23:59:59+00:00");
        int maxDays = 90;

        Random random = new Random(0);
        OffsetDateTime startTime = 
            computeRandomOffsetDateTime(
                earliestStartTime, latestStartTime, random);
        Duration duration = randomDaysDuration(maxDays, random);
        OffsetDateTime endTime = startTime.plus(duration);

        System.out.println("startTime " + startTime);
        System.out.println("endTime   " + endTime);        
        System.out.println("duration  " + duration.toDays() + " days");        
    }

    private static Duration randomDaysDuration(int maxDays, Random random)
    {
        int days = random.nextInt(maxDays);
        return Duration.ofDays(days);
    }

    private static OffsetDateTime computeRandomOffsetDateTime(
        OffsetDateTime min, OffsetDateTime max, Random random)
    {
        long differenceInSeconds = min.until(max, ChronoUnit.SECONDS);
        long seconds = nextLong(random, differenceInSeconds);
        return min.plusSeconds(seconds);
    }

    // From https://stackoverflow.com/a/2546186/3182664
    private static long nextLong(Random rng, long n) {
        // error checking and 2^x checking removed for simplicity.
        long bits, val;
        do {
           bits = (rng.nextLong() << 1) >>> 1;
           val = bits % n;
        } while (bits-val+(n-1) < 0L);
        return val;
     }    
}

The output is

startTime 2017-04-13T08:03:53Z
endTime   2017-06-01T08:03:53Z
duration  49 days

(Note: In the example, I computed the difference in seconds. Depending on your application case, you may want to use a different unit, for example, whole days)

Marco13
  • 53,703
  • 9
  • 80
  • 159