• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calendar shows incorrect date  RSS feed

 
nandish chitalia
Greenhorn
Posts: 7
  • 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
  • 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
Sheriff
Posts: 15865
81
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a formatter such as SimpleDateFormat to format dates:
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!