954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

GregorianCalendar

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;


java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Australia/Sydney"

etc etc

Many thanks in advance!

naffan
Light Poster
41 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

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
    System.out.println(sdf.format(c.getTime()));


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

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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?

naffan
Light Poster
41 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

Yes.
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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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
eg

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 :)

naffan
Light Poster
41 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

Actually,

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

naffan
Light Poster
41 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

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

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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?

naffan
Light Poster
41 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

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?

naffan
Light Poster
41 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

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");
System.out.println(sdf.format(myCal[0].getTime()));

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

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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!!!

naffan
Light Poster
41 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: