• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar shows incorrect date

 
nandish chitalia
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a simple code here

Calendar currentDate = Calendar.getInstance();

System.out.println("currentDate : " + currentDate.get(Calendar.DAY_OF_MONTH) + "/" + currentDate.get(Calendar.MONTH) + "/" + currentDate.get(Calendar.YEAR));

I executed this on the 7th of September 2007...i.e. 07/09/2007.
The output is

currentDate : 7/8/2007

Am I missing something fundamental here? Please help and thanks in advance.
 
bart zagers
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of the nice things about Calendar.
As documented in the api javadoc, months start at zero
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a formatter such as SimpleDateFormat to format dates:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic