0

I'am playing with Java Date and can't understand this:

Date myBirth = new Date(1991,01,21);
Log.d("DATE: ", "" + myBirth);

Here I initialized Date object. Why I get this output?

DEBUG/DATE:(31693): Sat Feb 21 00:00:00 EET 3891
  • 2
    January is month 0, February is month 1. – Nathan Villaescusa Oct 22 '12 at 22:49
  • 1
    You may wish to avoid writing numeric literals with a leading 0. A leading 0 tells Java to interpret the number as an octal. For numbers less than 8 this makes no difference. However 010 == 8. – Dunes Oct 22 '12 at 22:51
  • 1
    this constructor is deprecated. take a look at http://stackoverflow.com/questions/7661723/how-do-i-set-custom-date-in-android – vfcosta Oct 22 '12 at 22:59
  • Opposite http://stackoverflow.com/questions/13020507/cant-get-the-real-date-from-a-date-object-i-java – Steve Kuo Oct 22 '12 at 23:28

2 Answers2

3

From the Date docs:

  • A year y is represented by the integer y - 1900.
  • A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
1

Quoting from the Javadoc of this deprecated constructor of Date:

Parameters:
    year - the year **minus 1900**.
    month - the month between 0-11.
    date - the day of the month between 1-31.

So the output is what you ask, but not what you want.

fvu
  • 32,488
  • 6
  • 61
  • 79