Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about Calendar  RSS feed

 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calendar.add(calendar, 1) where calendar has a date of 08-30-2006, increments to 09-01-2006, instead of 08-31-2006. API not too clear to me. Can anyone help with this?
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this under the package java.util.Date ?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37234
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kwame,
By 08-30-06, do you mean that 8 is the month when printed out (August) or the number retrieved (September since Calendar uses 0 based indices.)

My guess would be the later. Then incrementing from Sept 30 to Oct 1 would make perfect sense since there is no Sept 31.
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean



where Calendar.DAY_OF_MONTH is August 30th
 
Paul Clapham
Sheriff
Posts: 22491
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd have to agree with Jeanne. Although the Calendar class has its idiosyncrasies, it's been around since 1997 and it doesn't have obvious bugs like what you describe. Therefore it's your misunderstanding; most likely you created the calendar with month = 8, i.e. September 30.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wrote a quick test program. And it worked fine. I started with Wednesday August 30th, and ended with Thursday August 31st.

Henry

 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys, you're absolutley right. The months start at index 0 instead of 1, so this needs to be adjusted for. Apparently I had August as 8.

Incidentally, why exactly is it it done this way? wouldn't it be a lot less confusing to just have the Months indexed from 1, like in the actual Gregorian calendar?!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kwame Iwegbue:
Incidentally, why exactly is it it done this way? wouldn't it be a lot less confusing to just have the Months indexed from 1, like in the actual Gregorian calendar?!

This is a design flaw that has been there since the early days of Java. Unfortunately it can't be repaired in a new version of Java without breaking compatibility with older versions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!