saving date to an array

adrianakw
adrianakw used Ask the Experts™
on
Hi,
  I am a newbie to java.
I would like to enquire, how to save a date to an array.

I did this;

Calendar today = Calendar.getInstance();
String test[] = new String [10];

test[0] = today.getTime();

Can anyone guide me??
Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Because Calendar.getInstance().getTime() returns a java.util.Date object, you should use'it in this way:

java.util.Calendar today = java.util.Calendar.getInstance();
java.util.Date[] date = new java.util.Date[] { today.getTime() };

System.out.println("Today is : " + date[0]);
Ovi

Commented:
Above comment is equivalent with the "extended" form :

java.util.Calendar today = java.util.Calendar.getInstance();
java.util.Date[] date = new java.util.Date[1];
date[0] = today.getTime();

System.out.println("Today is : " + date[0]);
Hi,
u can do
Date[] arrDates = new Date[10];
and then
arrDates[<index>]= today.getTime();

your problem is with
test[0] = today.getTime();
because test[0] is String instance and today.getTime() is a Date instance.

if u want to use String array u should do:
test[0] = today.getTime().toString(); // conver the Date to String

-gkern
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

or you might wana use date format ..

Calendar calen = Calendar.getInstance();
DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT);
String formattedDate = shortFormat.format (calen.getTime());

String date[] = new String[10];
date[0] = formattedDate;

the date will be in the form of mm/dd/yy .. or the way your computer settings display the date ..

:)
-=[sKySh@DoW]=-
you've got DateFormat.SHORT, .MEDIUM, .LONG, .FULL

forgot to tell u bout this .. hehee

:)
-=[sKySh@DoW]=-
>>or you might wana use date format ..

actually i don't recommend u to store your dates (your data/model) after formating it, i recommend u to store in in a Date array (as in the three first answers) and format in only before diplaying it (the view).
then the code will be this ..

Calendar calen = Calendar.getInstance();
DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT);
Date[] dateArray = new Date[10];

dateArray[0] = calen.getTime();

System.out.println (shortFormat.format(dateArray[0]));

:)
-=[sKySh@DoW]=-
Ovi

Commented:
skyshadow :
is not necesary to create an array of dates of length 10 since  you are putting there only one, representing current time. Is not necesary even an array for this.
java.util.Date[] date = new java.util.Date[] { today.getTime() };
-or-
java.util.Date[] date = new java.util.Date[1];
date[0] = today.getTime();
-or-
java.util.Date date = today.getTime();

, where today is of course Calendar.getInstance();

I've used java.util.Date to not make confusions with java.sql.Date class
Ovi:
i'm doing it only because in the beginning adrianakw did that .. and i thought he wana create an array with size 10 .. hehe

:)
-=[sKySh@DoW]=-

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial