Calendar get method

T

terloon

Hello,

I know there are a lot of issues with the Calendar class in Java (from
my searches anyway), but I can't seem to find one for my simple
question. When using the get methods of the calendar class, which
timezone is it using? I've changed the default time zone using
TimeZone.setDefault and have set the timezone of the Calendar object
itself, but it seems to keep returning values for a GMT timezone. Where
can I set it so the get methods of the Calendar class return the
correct values for a timezone I specify?

T.Ramos
 
D

Danno

terloon said:
Hello,

I know there are a lot of issues with the Calendar class in Java (from
my searches anyway), but I can't seem to find one for my simple
question. When using the get methods of the calendar class, which
timezone is it using? I've changed the default time zone using
TimeZone.setDefault and have set the timezone of the Calendar object
itself, but it seems to keep returning values for a GMT timezone. Where
can I set it so the get methods of the Calendar class return the
correct values for a timezone I specify?

T.Ramos

It's default uses the time zone of your machine....Try out some of this
code, and maybe it will help you with what you are looking for...

Calendar cal = Calendar.getInstance();
System.out.println(cal.getTimeZone());

//List of Ids, you'll need these to set the timezone.
for (String id : TimeZone.getAvailableIDs()) {
System.out.println(id);
}


TimeZone parisZone = TimeZone.getTimeZone("Europe/Paris");

DateFormat def =
DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG,
Locale.FRANCE);
def.setTimeZone(parisZone);

Calendar cal2 = Calendar.getInstance(parisZone);
System.out.println(cal2.get(Calendar.HOUR));
System.out.println(def.format(cal2.getTime()));
System.out.println(cal2.getTimeZone());
System.out.println(cal2.getTime()); //Careful toString() of
java.util.Date does things different
 
T

terloon

It's default uses the time zone of your machine....Try out some of this
code, and maybe it will help you with what you are looking for...

So does this mean that it doesn't matter how I set the TimeZone via
TimeZone.setDefault() or the setTimeZone method of the
GregorianCalendar object, it will always use the TimeZone of my
machine?
 
D

Danno

terloon said:
So does this mean that it doesn't matter how I set the TimeZone via
TimeZone.setDefault() or the setTimeZone method of the
GregorianCalendar object, it will always use the TimeZone of my
machine?

Check out the code, you can set the timezone of a Calendar object and
it works.
 
T

terloon

Danno said:
Check out the code, you can set the timezone of a Calendar object and
it works.

Oh bother, it does work. I had a setTimeZone hidden away setting it
back to GMT unbeknownst to me. Chalk this one up to carelessness.
 
D

Danno

terloon said:
Oh bother, it does work. I had a setTimeZone hidden away setting it
back to GMT unbeknownst to me. Chalk this one up to carelessness.

;) Happens to all of us.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top