• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to generate a random date with gregorian calendar?

 
michael montalbano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code I have so far is :



It is not working. I don't know how to get it so that it will be random.
 
michael montalbano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nevermind, just figured it out.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
michael montalbano wrote:year = call.nextInt(2010) + 2007;

This will generate a random year between 2007 and 4016. Is that really what you meant?
 
michael montalbano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah i changed it so that it's between 2007 and 2011 so it's good now. I need to have the date be formatted as, for example 'Thursday, 11/13/05 10:55pm' and it is currently formatted as Thur Jul 13 19:06:00 PDT 2005.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dates don't have formats. Strings have formats. Use SimpleDateFormatter to format a data any way you want.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
michael montalbano wrote: . . .
month = call.nextInt(11);
. . .
Don’t use number literals like that. UseI shall add code tags to your post, and you can see how much better it looks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how on earth do you get 11 to appear as July?

Another possibility is to create a String with the format method and the % tags, as seen here or here.
 
Carey Brown
Bartender
Posts: 1687
22
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will leave out December:
month = call.nextInt(11);

This doesn't work. Day starts with 1 not zero. This will generate a maximum day of 29 which will be wrong in almost all cases.
day = call.nextInt(30);

A better way would be to compute a random number of milli-seconds and convert that to a date. The limiting factor here is that the date will be >= Jan 1, 1970.

I suggest you read up on Random.nextInt() and GregorianCalendar.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael's code does produce random dates. It doesn't produce all dates with equal probability (as you say it is unable to produce some dates at all) but then that wasn't a stated requirement.

Your suggestion works much better at producing all dates with equal probability, but when you render those dates in a particular time zone then days when daylight saving time begin are chosen 23/24 as often as other days (since those days only have 23 hours). On the other hand you don't have that problem if you render them in UTC, so probably that would be the thing to do if this level of detail was important.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
michael montalbano wrote: . . .
month = call.nextInt(11);
. . .
Don’t use number literals like that. UseI shall add code tags to your post, and you can see how much better it looks.

That should be Since Calendar.JANUARY == 0 you could also use Calendar.DECEMBER + 1, but you need the + 1 or you'll never generate December itself.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right, Rob. Sorry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic