views:

44

answers:

4

I wrote this following java code to format the date and time in specific formats.You can see the below code at ideone .

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
class timeAndDateTransformation{
    public static void main(String[] argv){
            Calendar newDate = new GregorianCalendar(2009,7,1,15,20,00);
            SimpleDateFormat dateFormant = new SimpleDateFormat("yyyy/MM/dd");
            SimpleDateFormat timeFormant = new SimpleDateFormat("HH:mm:ss");
            System.out.println(dateFormant.format(newDate.getTime()).toString());
            System.out.println(timeFormant.format(newDate.getTime()).toString());
    }

}

Its giving me following output:

2009/08/01
15:20:00

In this output rest all it perfectly okay,except the month.I passed 7 as a month but in this for-matter output its giving 8 as a output.Please point me where am i doing wrong.I am not very familiar with the date/calendar classes of java,so please bear with me.

+6  A: 

Months are 0-based, you passed in 7 so that resolves to August.

From the api docs for java.util.Date:

A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.

It's really counter-intuitive to make the month zero-based. I think we've all gotten burned by that one at some point.

Nathan Hughes
ah.. do anybody know any specific reason behind it,because for days it's 1-31.In date/Time elements only day starts from 1,rest all are zero based,so more specific question might be,why they didn't design days also starting from 0-30 :) ?
Anil Vishnoi
You can use built-in constants like `Calendar.AUGUST` to avoid this problem.
Matt Solnit
@Anil: Yes, month is the sole 0-based field. for days and years the number is the only reference so subtracting one from it is blatantly awful, since months are primarily known by names (in English, anyway, as opposed to some languages like Japanese) they felt freer to change the numbering. Bad decision, though.
Nathan Hughes
+2  A: 

The month field in Java is zero based. GregorianCalendar.JANUARY is 0...etc etc. Therefore, if you want to pass in a date into the constructor, add one to the month value.

If you look at the [JavaDoc here][1], it explains it for you.

[1]: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html#set(int, int, int)

Codemwnci
+2  A: 

Humans like to see the first month (January) being 1, so that's what SimpleDateFormat does.

However computers like to see things starting from 0, and that's how GregorianCalendar manages the month param. See the constructors for GregorianCalendar and the description of the month parameter.

Adrian Smith
that is true, but it is confusing because the month field is the only field that is zero based.
Codemwnci
Yes, I agree! We are used to various things starting at zero in computing, I think, but months pretty much universally start with on. I think they made the wrong decision when designing that class. Anyway, it is the way it is, nothing we can do about it :(
Adrian Smith
+1  A: 

[Java considers January month 0. ][1] But when you output the month number with SimpleDateFormat it uses the standard January is month 1 system. So month 7 is outputted as 8.

If you are having trouble with the JDK's date and calendar consider using Joda Time, it's much easier

[1]: http://download.oracle.com/javase/1.4.2/docs/api/java/util/GregorianCalendar.html#GregorianCalendar(int, int, int)

Adam