Why don't I get the year using SimpleDateFormat in java?

I am trying to parse data in MySql format, I came across SimpleDateFormat . I can get the correct day and month, but I got a strange result for the year:

 date = 2009-06-22; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date d = sdf.parse(date); System.println(date); System.println(d.getDate()); System.println(d.getMonth()); System.println(d.getYear()); 

Outputs:

 2009-06-22 22 OK 5 Hum... Ok, months go from 0 to 11 109 o_O WTF ? 

I tried changing the format to YYYY-MM-dd (got an error) and yy-MM-dd (didn't do anything). I am programming on Android, I do not know if this is important.

I currently work around this using split, but it is dirty and does not allow me to use the i18n features.

+1
source share
2 answers

Year relative to 1900. This is a "feature" of the Date class. Try using Calender .

+12
source

Thanks to Aaron, the correct version is:

 Calendar c = Calendar.getInstance(); c.setTime(sdf.parse(date)); System.println(c.get(Calendar.YEAR)); 
+5
source

All Articles