-
Creating an Array of Dates
Does anyone know if there is a way to create an array of dates in the same line of code?
Date hiredDate[] = { new Date(105, 2, 15), new Date(99, 7, 26) };
Works find in J#, but will not work with Java SDK 1.4 (or atleast with the version I have).
When I compile it, I receive the following messege (and nothing works):
Note: F:\Project1Tester.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
Tool completed successfully
-
That's because as per the Java API documentation for Date, the constructor Date(int year, int month, int day) is deprecated (that means marked as obsolete and might be removed from future releases). There is also a relevant comment in the documentation, which is the way you should do it.
As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).
Happiness is good health and a bad memory.
-
Thank you aniseed.
Using the Calendar methods does work, but requires me to set the date first, then I can set the array value from the Calendar object. (Currently, that is what the program does do).
aCalendar.set(2005, Calendar.MARCH, 15);
hiredDate[0] = aCalendar.getTime();
aCalendar.set(1999, Calendar.AUGUST, 26);
hiredDate[1] = aCalendar.getTime();
aCalendar.set(1957, Calendar.JANUARY, 5);
hiredDate[2] = aCalendar.getTime();
But, what I was wondering, is there a way to create the array (of dates) and set its values in a single line of code. Something like this?
Date hiredDate[] = { new Date(105, 2, 15), new Date(99, 7, 26), new Date(57, 0, 5) };
-
make the Date class by yourself and write all the methods for it =D
or make a method that sets the date to a certain time and return the date for it
like:
Code:
private static getDate(int year, int month, int day){
aCalendar.set(year, month, day);
return aCalender.getTime();
}
whatever aCalender is...
and then do
Date hiredDate[] = { getDate(1999, Calender.AUGUST, 26), };
Last edited by skyuzo; 02-03-2007 at 10:41 PM.
-
Thank you skyuzo,
That seems to do the trick (short of the typo... Got to love computers. the aCalender.getTime() should have been aCalendar.getTime())
And aCalendar is just an instance of the Java Clendar class.
private static getDate(int year, int month, int day)
{
aCalendar.set(year, month, day);
return aCalendar.getTime();
}
Similar Threads
-
By Tmcclain in forum Java
Replies: 7
Last Post: 02-13-2009, 10:57 PM
-
Replies: 3
Last Post: 06-09-2006, 11:01 AM
-
Replies: 6
Last Post: 11-01-2005, 09:05 AM
-
Replies: 3
Last Post: 06-13-2001, 08:57 PM
-
By Bennett in forum ASP.NET
Replies: 0
Last Post: 10-03-2000, 03:57 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks