fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.util.concurrent.ThreadLocalRandom ;
  8. import java.util.TreeMap ;
  9.  
  10. import java.time.*;
  11. import java.time.format.*;
  12. import java.time.temporal.*;
  13.  
  14. /* Name of the class has to be "Main" only if the class is public. */
  15. class Ideone
  16. {
  17. public static void main (String[] args) throws java.lang.Exception
  18. {
  19. Ideone app = new Ideone();
  20. app.doIt();
  21. }
  22.  
  23. public void doIt() {
  24. ZoneId z = ZoneId.of( "America/Montreal" ) ;
  25. int count = 10 ;
  26. LocalDate today = LocalDate.now( z );
  27. LocalDate laterDate = today.plusDays( count );
  28. Instant start = today.atStartOfDay( z ).toInstant();
  29. Instant stop = laterDate.atStartOfDay( z ).toInstant();
  30.  
  31. // Collect the frequency of each date. We want to see bias towards earlier dates.
  32. List<LocalDate> dates = new ArrayList<>( count );
  33. Map<LocalDate , Integer > map = new TreeMap<LocalDate , Integer >();
  34. for( int i = 0 ; i <= count ; i ++ ) {
  35. LocalDate localDate = today.plusDays( i ) ;
  36. dates.add( localDate ); // Increment to next date and remember.
  37. map.put( localDate , new Integer( 0 ) ); // Prepopulate the map with all dates.
  38. }
  39. for( int i = 1 ; i <= 10_000 ; i ++ ) {
  40. Instant instant = this.getRandomInstantBetween( start , stop );
  41. LocalDate localDate = instant.atZone( z ).toLocalDate();
  42. Integer integer = map.get( localDate );
  43. map.put( localDate , integer + 1); // Increment to count each time get a hit on this date.
  44. }
  45. System.out.println( map );
  46. }
  47.  
  48. public Instant getRandomInstantBetween( Instant early , Instant late) {
  49.  
  50. Duration duration = Duration.between( early , late ) ;
  51. // Assert the duration is positive or zero: ( ! duration.isNegative() )
  52.  
  53. long bound = duration.toNanos() ;
  54. ThreadLocalRandom random = ThreadLocalRandom.current() ;
  55. long nanos1 = random.nextLong( 0 , bound );
  56. long nanos2 = random.nextLong( 0 , bound );
  57. long nanos = Math.min( nanos1 , nanos2 ); // Select the lesser number.
  58. Instant instant = early.plusNanos( nanos );
  59.  
  60. return instant;
  61. }
  62. }
Success #stdin #stdout 0.23s 4386816KB
stdin
Standard input is empty
stdout
{2017-02-24=1853, 2017-02-25=1697, 2017-02-26=1548, 2017-02-27=1255, 2017-02-28=1130, 2017-03-01=926, 2017-03-02=706, 2017-03-03=485, 2017-03-04=299, 2017-03-05=101, 2017-03-06=0}