Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the current year in the date saved as 3912?

to get the current date and time

final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

to create current date object

Date toDate;
     toDate.setYear(mYear);
     toDate.setMonth(mMonth);
     toDate.setDate(mDay);



Date endDate = toDate;

when printing endDate object I got

Mon Jan 01 13:11:00 GMT+03:00 3912

why ?

like image 407
Bader Avatar asked Jan 01 '12 11:01

Bader


1 Answers

From Date.setYear(int) description: Sets the gregorian calendar year since 1900 for this Date object. Thus, 1900 + 2012 = 3912.

But calendar.get(Calendar.YEAR) returns exact year number 2012. So this inconsistency of API causes your issue. But anyway Date.setYear(int) is deprecated, thus, it is better to use Calendar object for date calculations.

like image 113
Aleksejs Mjaliks Avatar answered Nov 19 '22 09:11

Aleksejs Mjaliks