• Post Reply Bookmark Topic Watch Topic
  • New Topic

calendar error  RSS feed

 
Abigail Decan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i expected on output of

3, since it's March.

but somehow i'm getting a 2.

i have no clue how to fix this and what i'm doing wrong.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, months are zero-based in class Calendar. So, 0 = January, 1 = February, 2 = March, etc., until 11 = December. That's just how class Calendar was designed.

Note: You do not need to declare that your class might throw ClassNotFoundException. Remove line 10.
 
Abigail Decan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i see, thanks!


and i cleaned my code. thanks for the tip
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should learn to read the java API. This has all the documentation for the predefined classes, including the Calendar class. It states in there:

MONTH
public static final int MONTH
Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.
(emphasis added by me)

Usually, when I'm coding, I always have that page open to check and see what things do.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!