Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Ho do I fix Gregorian Calender Displaying wring date

Hi :)the code below will result in this being printed to screen:
Today Year: 2005
Today Month: 8
Today Day: 13
Today Hour: 8
Today Minute: 59
Today date: 2005-8-13

Yet the actual date is 2005-9-13. Can anyone tell me why the month keeps comeing out wrong?

import java.util.*;
import java.util.Date;
import java.text.*;
import java.text.DateFormat;

public class dates{
public static void main(String[] args){
Calendar today = Calendar.getInstance();
System.out.println("Today Year: " + today.get(Calendar.YEAR));
System.out.println("Today Month: " + today.get(Calendar.MONTH));
System.out.println("Today Day: " + today.get(Calendar.DAY_OF_MONTH));
System.out.println("Today Hour: " + today.get(Calendar.HOUR));
System.out.println("Today Minute: " + today.get(Calendar.MINUTE));

System.out.println("Today date: " + today.get(Calendar.YEAR)+ "-" + today.get(Calendar.MONTH) + "-" + today.get(Calendar.DAY_OF_MONTH));
}
}
[1063 byte] By [Hello] at [2007-11-11 6:49:50]
# 1 Re: Ho do I fix Gregorian Calender Displaying wring date
Calendar months are zero-indexed (e.g., January = 0, February = 1, etc.). The results you receive are correct. If you want to format the output of the date, use java.text.DateFormat and/or java.text.SimpleDateFormat.

Formatting a Date Using a Custom Format (http://www.javaalmanac.com/egs/java.text/FormatDate.html)
yawmark at 2007-11-11 22:40:12 >