0

In Android Application, I am trying to retrieve the current month and year, by using the following code.

setMonth(Calendar.getInstance().getTime().getMonth());
setYear(Calendar.getInstance().getTime().getYear());
System.out.println("MONTH sssssssssss:" + Month);
System.out.println("YEAR ssssssssssss:" +  Year);

output is

04-20 16:36:43.723: I/System.out(4718): MONTH sssssssssss:3
04-20 16:36:43.723: I/System.out(4718): YEAR ssssssssssss:114

Could someone please help me to correct year value?

codey modey
  • 983
  • 2
  • 10
  • 23

1 Answers1

5

Do the following instead

Calendar today = Calendar.getInstance();
int month = today.get(Calendar.MONTH); // january == 0 btw
int year = today.get(Calendar.YEAR);
// do your printing here

The reason you are getting 114 is because the getTime() method returns a java.util.Date class, and by default, the java.util.Date class has its year value offset by 1900 (don't ask me why). This means that the current year would be 2014 - 1900 = 114, which is what you are seeing.

Tamby Kojak
  • 2,129
  • 1
  • 14
  • 25
  • Here is a related SO thread: http://stackoverflow.com/questions/7215621/why-does-javas-date-getyear-return-111-instead-of-2011 – Tamby Kojak Apr 20 '14 at 20:50