?
Solved

Problem constructing java.util.Date object

Posted on 2008-11-20
10
Medium Priority
?
415 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:dsmclaughlin
  • 6
  • 2
  • 2
10 Comments
 
LVL 9

Expert Comment

by:brunoguimaraes
ID: 23004005
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
 
LVL 9

Expert Comment

by:brunoguimaraes
ID: 23004026
Where you read Calencar, that should be Calendar.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23004047
Easier would be


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

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 23004173
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 23004233
Plus you have a typo:

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

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

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23004271
(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
 

Author Comment

by:dsmclaughlin
ID: 23004392
@ 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 23004439
Date works on *milliseconds* not seconds
0
 

Author Comment

by:dsmclaughlin
ID: 23004459
ahhhh good man! cheers! :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23004494
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question