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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With