• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Gregorian Calendar

 
Alan Morgan
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

I am having a really strange problem using the Gregorian Calendar and I was hoping someone could enlighten me as to what is going on.

Ok so I pass a year, month and day when creating a Gregorian Calendar and then I set the TimeZone to GMT + 12 as so:

GregorianCalendar roomCal = new GregorianCalendar(year, month, day);
roomCal.setTimeZone(TimeZone.getTimeZone("GMT+12"));

With the code above I get the following output:

year = 2005
month = 3
day = 10
roomDate = Sat Apr 09 13:00:00 BST 2005

Obviously the month, day and timezone are wrong.

However if I change the code to:

GregorianCalendar roomCal = new GregorianCalendar(year, month-1, day+1);
roomCal.setTimeZone(TimeZone.getTimeZone("GMT+12"));

I get the following output:

year = 2005
month = 3
day = 10
roomDate = Thu Mar 10 12:00:00 GMT 2005

So month,day and timezone now ok again.

What is going on ?

Thanks,
Alan.
 
santosh kulkarni
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Month is zero based so April is 3
TimeZone is not recognizing the expression (probably) so defaulting to GMT
don't know about the date
HTH
Santosh

From the javadoc
GregorianCalendar
public GregorianCalendar(int year,
int month,
int date)Constructs a GregorianCalendar with the given date set in the default time zone with the default locale.

Parameters:
year - the value used to set the YEAR time field in the calendar.
month - the value used to set the MONTH time field in the calendar. Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field in the calendar.
[ November 22, 2005: Message edited by: santosh kulkarni ]
 
muhd yousuf
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by santosh kulkarni:
Month is zero based so April is 3
TimeZone is not recognizing the expression (probably) so defaulting to GMT
don't know about the date
HTH
Santosh

From the javadoc
GregorianCalendar
public GregorianCalendar(int year,
int month,
int date)Constructs a GregorianCalendar with the given date set in the default time zone with the default locale.

Parameters:
year - the value used to set the YEAR time field in the calendar.
month - the value used to set the MONTH time field in the calendar. Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field in the calendar.

[ November 22, 2005: Message edited by: santosh kulkarni ]


Ok zero-based month explains why I need month - 1.

But anyone know why day + 1 is necessary ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic