Creating an Array of Dates

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Creating an Array of Dates

  1. #1
    Join Date
    Feb 2007

    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:\ uses or overrides a deprecated API.
    Note: Recompile with -deprecation for details.

    Tool completed successfully

  2. #2
    Join Date
    Oct 2005
    New Jersey
    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.

  3. #3
    Join Date
    Feb 2007
    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) };

  4. #4
    Join Date
    Apr 2006
    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

    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 11:41 PM.

  5. #5
    Join Date
    Feb 2007
    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

  1. Reversing an Array
    By Tmcclain in forum Java
    Replies: 7
    Last Post: 02-13-2009, 11:57 PM
  2. Creating an ex-or table in an array
    By Sammy in forum C++
    Replies: 3
    Last Post: 06-09-2006, 12:01 PM
  3. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 10:05 AM
  4. creating an array of classes
    By Jim in forum Java
    Replies: 3
    Last Post: 06-13-2001, 09:57 PM
  5. Replies: 0
    Last Post: 10-03-2000, 04: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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center