| 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
Bartender
Joined: Aug 16, 2005
Posts: 12956
|
|
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: 56554
|
|
|
Dates don't have formats. Strings have formats. Use SimpleDateFormatter to format a data any way you want.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32838
|
|
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: 32838
|
|
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: 161
|
|
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: 16487
|
|
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: 19232
|
|
Campbell Ritchie wrote:
michael montalbano wrote: . . .
month = call.nextInt(11);
. . .
Don’t use number literals like that. Use I 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
How To Ask Questions How To Answer Questions
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32838
|
|
|
Yes, you are right, Rob. Sorry.
|
 |
 |
|
|
subject: Is it possible to generate a random date with gregorian calendar?
|
|
|