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

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: 15082

Use a formatter such as SimpleDateFormat to format dates:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Calendar shows incorrect date
jQuery in Action, 3rd edition