dsmclaughlin
asked on
Problem constructing java.util.Date object
Hi,
I'm trying to construct a java.util.Date object to be a date very far in the future but I'm having problems.
To construct the date 01/01/3000 I tried the following:
new Date (86400 * 365 * 1030)
that is, the number of seconds in a day * the number of days in a year * the number of years (3000 - 1970 = 1030) but this gives me the date 21/10/1969. I thought that the Date constructor allowed you to construct a date by supplying the number of milliseconds elapsed since 01/01/1970 but this doesn't seem to work. Can anyone help?
Many thanks
~drew
I'm trying to construct a java.util.Date object to be a date very far in the future but I'm having problems.
To construct the date 01/01/3000 I tried the following:
new Date (86400 * 365 * 1030)
that is, the number of seconds in a day * the number of days in a year * the number of years (3000 - 1970 = 1030) but this gives me the date 21/10/1969. I thought that the Date constructor allowed you to construct a date by supplying the number of milliseconds elapsed since 01/01/1970 but this doesn't seem to work. Can anyone help?
Many thanks
~drew
Where you read Calencar, that should be Calendar.
Easier would be
Date d = new GregorianCalendar(3000, 1, 1).getTime();
This might give you a clue as to your error: instead of
>>new Date (86400 * 365 * 1030)
try
>>new Date (86400 * 365 * 1030)
try
new Date (86400L * 365 * 1030)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(So best to try my 2nd correction first - it will show you that the real reason for the error lies in the issues around my first correction)
ASKER
@ CEHJ why is 86400 a typo other than for the fact I left the L off to make it a long? Google tells me that there are 86400 seconds in a day....I'm slightly confused.
Date works on *milliseconds* not seconds
ASKER
ahhhh good man! cheers! :)
You might be surprised to find that the latest date that can be constructed using int is
Sun Jan 25 21:31:23 GMT 1970
;-)
Sun Jan 25 21:31:23 GMT 1970
;-)
Open in new window