Hi all,

Quick question,

Is there a way to store a GregorianCalendar object in a hard-coded array?
Such as you do an int or String ?

Or do I have to send 3 ints for dd/mm/yyyy
and then create a new GregorianCalendar object in the Constructor?

If this is the case, how would I format the output ?
If i directly send the new object to Console it spits out a LOT of stuff
like the following;


etc etc

Many thanks in advance!

Why not just create an array of GregorianCalendar instances, exactly like an array of Strings?

GregorianCalendar[] myCal = new GregorianCalendar[99];
myCal[0] = Calendar.getInstance();
// etc

To output a GregorianCalendar instance in any specific text format you can imagine, use SimpleDateFormat, eg like this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    Calendar c = Calendar.getInstance(); // today

or use the java.util.Formatter class if you like C's printf approach

I've been advised I need to create a GregorianCalendar object within an array
(A date of birth)

As I have been reading it closer and realising I can't send 3 ints I MUST send a gregoriancalendar object, it seems I have to change a String to a date, then store the date in the array, and then to output that GregorianCalendar object, I need to then convert it back to a String to output to the console.

Does this seem reasonable?

Input and output formats vary with locale, console vs GUI, user preferences etc, but a date is a date is a date regardless. Store the date and you're safe. Let people input them and view them however they want.

Thankyou James,

Unfortunately whilst still in training I need to follow exactly what my Lecturer tells me to do :P

I just created Gregorian Objects and passing them directly into the array

GregorianCalendar dob1 = new GregorianCalendar(1980,1,1);

Then I have to convert them to string's when they get read from the array..
With simpledateformat, thanks for your time as always James :)


Can you see any error with this logic?
dob being the GregorianCalendar object in the array

public String getDob(GregorianCalendar dob) 
		SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
		birthDate = sdf.format(dob);
		return this.birthDate;

I'm getting the Cannot format object as date

Check the example in my previous post - you missed a method call

Hmm, I tried what you said to test if it was my "dob" object
I can get todays date to print without a problem when I do your method (which is great),
but I'm not sure how to implement that calendar call when I already have called it by making the dob object?

Does that make sense?

Am I allowed to do this ?

GregorianCalendar dob1 = new GregorianCalendar(1980,1,1);

Then pass that into an array, and then simpledateformat back to String?
Is that possible?

Yes, it really is just the same as any other array, so you can do stuff like:

GregorianCalendar[] myCal = new GregorianCalendar[99];
myCal[0] = new GregorianCalendar(1980,1,1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

Just experiment a bit with it. Everything you know about arrays of Strings applies to arrays of GregorianCalendar also.

Ahhh, you were absolutely spot on, after fiddling with that for only around 10 minutes I managed to get it all working, you are seriously a genius James, thank you so much!!!