aspose file tools*
The moose likes Beginning Java and the fly likes Calendar shows incorrect date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar shows incorrect date" Watch "Calendar shows incorrect date" New topic
Author

Calendar shows incorrect date

nandish chitalia
Greenhorn

Joined: Apr 11, 2006
Posts: 7
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

Joined: Feb 05, 2003
Posts: 234
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

Joined: Aug 16, 2005
Posts: 14104
    
  16

Use a formatter such as SimpleDateFormat to format dates:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calendar shows incorrect date