郭玉龙 郭玉龙 - 1 month ago 10
Android Question

Is this a java.util.Date bug?

I have tried to create a

java.util.Date
object by its constructor
Date(int year, int month, int day, int hours, int minutes, int seconds)
.
But some strange result from
java.util.Date.getDay()
, is it a bug in Android SDK?

Log.e("Day:", "" + temp_day);
java.util.Date date = new java.util.Date(temp_year, temp_month, temp_day, temp_hour, temp_minute, temp_second);
Log.e("GetDay():", date.getDay() + "");


And here is logs from LogCat.

10-22 20:13:06.305: E/Day:(3323): 17

10-22 20:13:06.305: E/GetDay():(3323): 5

Answer

getDay() returns the day of the week. You meant to use (the IMHO badly named) getDate() method which returns the day of the month.

Regardless, note that both these methods, as well the the Date constructor you're using are deprecated. If you want to copy dates around, you'd be better served to use Calendar objects:

Calendar tempCal = // get from somewhwre...
Calendar newCal = Calendar.getInstance();
newCal.setTimeInMillis(tempCal.getTimeInMillis());
Comments