Take the 2-minute tour ×

I realized this:

Calendar c = new GregorianCalendar();

And this:

Date d = c.getTime();
int day = d.getDay();
int month = d.getMonth();
int year= d.getYear();

Yield different numbers, is there some initialization I am missing? I care because I am using ORMLite and trying to store dates into a database, which is a Date object, but Date is being deprecated and so I am trying now to use Calendar but it doesn't seem that simple since the above code results in different answers for the day, month, and year.

share|improve this question
Im curious, what values are you getting for both? –  RMT Jul 28 '11 at 15:28
add comment

1 Answer

up vote 8 down vote accepted

the Date.getYear(), getMonth() and getDay() are deprecated and specifically ask you to use Calendar.get()

The reason why you are getting different answers is two-fold

getDay() returns the day of the week and not the date -- use getDate() here

getYear() returns year - 1900

Here is the relevant note from the API documentation

Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.


share|improve this answer
I would up this 100 times if I could, good answer –  RMT Jul 28 '11 at 15:39
Maybe there is also problem with timezone and DST - they may be differently handled by Calendar and Date. –  Tadeusz Kopec Jul 28 '11 at 15:41
Indeed great answer, but now I am curious about something...what about Date fields in databases ? That ORMLite i just got working on android great and its pretty awesome, but dealing with the date fields is not very fun. Should I just make longs and store the time in millis there and then write some utility for displaying dates but always store those millis? –  Codejoy Jul 28 '11 at 15:42
@Codejoy -- not familiar with ORMLite .. but storing dates as dates makes more sense to me. If you store them as millis, you might have trouble querying them later ( or even making sense of them ). –  Kal Jul 28 '11 at 15:49
add comment

Your Answer


