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

dsmclaughlinAsked:
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.

brunoguimaraesCommented:
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

0
brunoguimaraesCommented:
Where you read Calencar, that should be Calendar.
0
CEHJCommented:
Easier would be


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

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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

>>new Date (86400 * 365 * 1030)

try
new Date (86400L * 365 * 1030) 

Open in new window

0
CEHJCommented:
Plus you have a typo:

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

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

Open in new window

0

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
CEHJCommented:
(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)
0
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.
0
CEHJCommented:
Date works on *milliseconds* not seconds
0
dsmclaughlinAuthor Commented:
ahhhh good man! cheers! :)
0
CEHJCommented:
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

;-)
0
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
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.