Problem constructing java.util.Date object


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


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try the following.
Calencar c = Calendar.getInstance();
c.set(Calendar.YEAR, 3000);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DAY_OF_MONTH, 1);
Date d = c.getTime();

Open in new window

Where you read Calencar, that should be Calendar.
Easier would be

Date d = new GregorianCalendar(3000, 1, 1).getTime();

Open in new window

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

This might give you a clue as to your error: instead of

>>new Date (86400 * 365 * 1030)

new Date (86400L * 365 * 1030) 

Open in new window

Plus you have a typo:

>>new Date (86400L * 365 * 1030)

should be
new Date (86400000L * 365 * 1030)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
(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)
dsmclaughlinAuthor Commented:
@ 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
dsmclaughlinAuthor Commented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.