I'm a little confused. I've been working with the Calendar and GregorianCalendar classes in Java and I have been printing the hour, but it says 4, and it is 11 where I am now.
My question is, is the hour based on the local time? Is it based on a specific time zone?
I've looked around, but I haven't found a clear answer.
Calendar today = new GregorianCalendar();
int y = today.get(Calendar.YEAR);
int h = today.get(Calendar.HOUR);
int ap = today.get(Calendar.AM_PM);
System.out.println(y + " " + h + " " + ap);
And my output:
2013 4 1
The no-arg constructor of GregorianCalender uses
Timzone.getDefault()
as its timezone. This is the timezone (or in other words: the local time) of the JVM executing your code. All of the calendar fields, includingCalendar.HOUR
, are based on this timezone.If this differs from your local time, then either the JVM is in a different timezone or the JVM (or rather its hosting computer) is configured incorrectly.