I have faced the same problem many times.
The Same Problem was With This Question and Got Solution Like the Same,
How to compare known hours ad current hour in android?
Problem :
When I use Calendar calCurr = Calendar.getInstance(); to get the Calendar object of current date and time, It always return me wrong.
I have put logs and checked it and to make it correct I had to add in years and months and then I got the correct object for Current Date and Time.
See My Example :
Calendar calCurr = Calendar.getInstance();
Log.i("Time in mili of Current - Normal", ""+calCurr.getTimeInMillis());
// see what it gives? dont know why?
Date date = new Date();
calCurr.set(date.getYear()+1900, date.getMonth()+1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
// so added one month to it
Log.i("Time in mili of Current - after update", ""+calCurr.getTimeInMillis());
// now get correct
Question :
- Why it’s giving the wrong output?
- Is it a bug in there or My concept about the Calendar class is wrong?
- tell me what should have been done
for that?
It works perfectly as expected if you change to
getDate()it outputs :What do you expect ? And in milleseconds it also equals 30 days :
and the calculation is (difference, divided by milliseconds in a day) :
And todays date in milliseconds after 1970 is 1333536754 … which fits, I don’t see a problem.
EDIT
Your Problem is you are setting Month like 3 for march…there you need to set 2..cause months are indexed from 0 to 11.