File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar shows incorrect date" Watch "Calendar shows incorrect date" New topic

Calendar shows incorrect date

nandish chitalia

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

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:
subject: Calendar shows incorrect date
Similar Threads
getting system date
Date validation
Why they have named a calendar method like this?
setTime() function not setting the time