Link to home
Start Free TrialLog in
Avatar of dsmclaughlin
dsmclaughlinFlag for United Kingdom of Great Britain and Northern Ireland

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

Avatar of brunoguimaraes
brunoguimaraes
Flag of Brazil image

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.
Avatar of CEHJ
Easier would be


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

Open in new window

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

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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)
Avatar of dsmclaughlin

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

;-)