wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Is it possible to generate a random date with gregorian calendar? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is it possible to generate a random date with gregorian calendar?" Watch "Is it possible to generate a random date with gregorian calendar?" New topic
Author

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

michael montalbano
Greenhorn

Joined: Oct 17, 2011
Posts: 4
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

Joined: Oct 17, 2011
Posts: 4
nevermind, just figured it out.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14143
    
  18

michael montalbano wrote:year = call.nextInt(2010) + 2007;

This will generate a random year between 2007 and 4016. Is that really what you meant?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
michael montalbano
Greenhorn

Joined: Oct 17, 2011
Posts: 4
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

Joined: Jan 10, 2002
Posts: 61187
    
  66

Dates don't have formats. Strings have formats. Use SimpleDateFormatter to format a data any way you want.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
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

Joined: Oct 13, 2005
Posts: 38756
    
  23
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
Ranch Hand

Joined: Nov 19, 2001
Posts: 174

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
Bartender

Joined: Oct 14, 2005
Posts: 18563
    
    8

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

Joined: Oct 27, 2005
Posts: 19692
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
Yes, you are right, Rob. Sorry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to generate a random date with gregorian calendar?